From 066241c535ca8e342f9790872680995e8b82b38a Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 13:02:23 +0200 Subject: [PATCH 1/7] fix: support NYC cwd option for file pattern matching - Add support for NYC 'cwd' option in findSourceFiles function - Pass cwd option to globby for proper file pattern resolution - Add test case 'all-files-cwd' to verify cwd functionality - Update CI configuration to include new test case in matrix This fix ensures that when NYC is configured with a 'cwd' option, the file patterns are resolved relative to the specified directory instead of the current working directory. --- .circleci/config.yml | 1 + task-utils.js | 8 ++++++- test-apps/all-files-cwd/.babelrc | 3 +++ test-apps/all-files-cwd/README.md | 1 + test-apps/all-files-cwd/cypress.config.js | 11 ++++++++++ .../all-files-cwd/cypress/e2e/spec.cy.js | 12 ++++++++++ .../all-files-cwd/cypress/plugins/index.js | 4 ++++ .../all-files-cwd/cypress/support/commands.js | 1 + .../all-files-cwd/cypress/support/e2e.js | 1 + test-apps/all-files-cwd/index.html | 17 ++++++++++++++ test-apps/all-files-cwd/main.js | 3 +++ test-apps/all-files-cwd/package.json | 22 +++++++++++++++++++ test-apps/all-files-cwd/second.js | 7 ++++++ 13 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 test-apps/all-files-cwd/.babelrc create mode 100644 test-apps/all-files-cwd/README.md create mode 100644 test-apps/all-files-cwd/cypress.config.js create mode 100644 test-apps/all-files-cwd/cypress/e2e/spec.cy.js create mode 100644 test-apps/all-files-cwd/cypress/plugins/index.js create mode 100644 test-apps/all-files-cwd/cypress/support/commands.js create mode 100644 test-apps/all-files-cwd/cypress/support/e2e.js create mode 100644 test-apps/all-files-cwd/index.html create mode 100644 test-apps/all-files-cwd/main.js create mode 100644 test-apps/all-files-cwd/package.json create mode 100644 test-apps/all-files-cwd/second.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 317abe60c..d70ae4bc7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -145,6 +145,7 @@ workflows: parameters: jobname: - all-files + - all-files-cwd - backend - batch-send-coverage - before-all-visit diff --git a/task-utils.js b/task-utils.js index 7e39cfdef..945b349c2 100644 --- a/task-utils.js +++ b/task-utils.js @@ -293,6 +293,7 @@ function tryFindingLocalFiles(nycFilename) { function findSourceFiles(nycOptions) { debug('include all files options: %o', { all: nycOptions.all, + cwd: nycOptions.cwd, include: nycOptions.include, exclude: nycOptions.exclude, extension: nycOptions.extension @@ -330,7 +331,12 @@ function findSourceFiles(nycOptions) { debug('searching files to include using patterns %o', patterns) - const allFiles = globby.sync(patterns, { absolute: true }) + const globbyOptions = { absolute: true } + if (nycOptions.cwd) { + globbyOptions.cwd = nycOptions.cwd + } + const allFiles = globby.sync(patterns, globbyOptions) + return allFiles } /** diff --git a/test-apps/all-files-cwd/.babelrc b/test-apps/all-files-cwd/.babelrc new file mode 100644 index 000000000..7a016cf8e --- /dev/null +++ b/test-apps/all-files-cwd/.babelrc @@ -0,0 +1,3 @@ +{ + "plugins": ["istanbul"] +} diff --git a/test-apps/all-files-cwd/README.md b/test-apps/all-files-cwd/README.md new file mode 100644 index 000000000..e5faf2889 --- /dev/null +++ b/test-apps/all-files-cwd/README.md @@ -0,0 +1 @@ +# example: all files diff --git a/test-apps/all-files-cwd/cypress.config.js b/test-apps/all-files-cwd/cypress.config.js new file mode 100644 index 000000000..7b34c2f8e --- /dev/null +++ b/test-apps/all-files-cwd/cypress.config.js @@ -0,0 +1,11 @@ +const { defineConfig } = require('cypress') + +module.exports = defineConfig({ + fixturesFolder: false, + e2e: { + setupNodeEvents(on, config) { + return require('./cypress/plugins/index.js')(on, config) + }, + baseUrl: 'http://localhost:1234' + } +}) diff --git a/test-apps/all-files-cwd/cypress/e2e/spec.cy.js b/test-apps/all-files-cwd/cypress/e2e/spec.cy.js new file mode 100644 index 000000000..d7ca62b2f --- /dev/null +++ b/test-apps/all-files-cwd/cypress/e2e/spec.cy.js @@ -0,0 +1,12 @@ +/// +it('works', () => { + cy.visit('/') + cy.contains('Page body') + + cy.window() + .invoke('reverse', 'super') + .should('equal', 'repus') + + // application's code should be instrumented + cy.window().should('have.property', '__coverage__') +}) diff --git a/test-apps/all-files-cwd/cypress/plugins/index.js b/test-apps/all-files-cwd/cypress/plugins/index.js new file mode 100644 index 000000000..101311b68 --- /dev/null +++ b/test-apps/all-files-cwd/cypress/plugins/index.js @@ -0,0 +1,4 @@ +module.exports = (on, config) => { + require('@cypress/code-coverage/task')(on, config) + return config +} diff --git a/test-apps/all-files-cwd/cypress/support/commands.js b/test-apps/all-files-cwd/cypress/support/commands.js new file mode 100644 index 000000000..cc6040de7 --- /dev/null +++ b/test-apps/all-files-cwd/cypress/support/commands.js @@ -0,0 +1 @@ +import '@cypress/code-coverage/support' diff --git a/test-apps/all-files-cwd/cypress/support/e2e.js b/test-apps/all-files-cwd/cypress/support/e2e.js new file mode 100644 index 000000000..b5c578c9d --- /dev/null +++ b/test-apps/all-files-cwd/cypress/support/e2e.js @@ -0,0 +1 @@ +require('./commands') diff --git a/test-apps/all-files-cwd/index.html b/test-apps/all-files-cwd/index.html new file mode 100644 index 000000000..993f0c189 --- /dev/null +++ b/test-apps/all-files-cwd/index.html @@ -0,0 +1,17 @@ + + Page body + + + + diff --git a/test-apps/all-files-cwd/main.js b/test-apps/all-files-cwd/main.js new file mode 100644 index 000000000..5dd69be2f --- /dev/null +++ b/test-apps/all-files-cwd/main.js @@ -0,0 +1,3 @@ +window.add = (a, b) => a + b + +window.sub = (a, b) => a - b diff --git a/test-apps/all-files-cwd/package.json b/test-apps/all-files-cwd/package.json new file mode 100644 index 000000000..dc1b4a5f4 --- /dev/null +++ b/test-apps/all-files-cwd/package.json @@ -0,0 +1,22 @@ +{ + "name": "example-all-files-cwd", + "description": "Report all files cwd", + "private": true, + "scripts": { + "cy:run": "cypress run", + "start": "parcel serve index.html", + "start:windows": "npx bin-up parcel serve index.html", + "pretest": "rimraf .nyc_output .cache coverage dist", + "test": "start-test 1234 cy:run", + "coverage:verify": "npx nyc report --check-coverage true --lines 100", + "coverage:check-files": "check-coverage main.js && check-coverage second.js && check-coverage not-covered.js && check-coverage cypress.config.js && only-covered --from coverage/coverage-final.json main.js second.js not-covered.js cypress.config.js" + }, + "nyc": { + "all": true, + "cwd": "../all-files", + "include": "*.js" + }, + "devDependencies": { + "@babel/core": "^7.22.15" + } +} diff --git a/test-apps/all-files-cwd/second.js b/test-apps/all-files-cwd/second.js new file mode 100644 index 000000000..494a0c5fc --- /dev/null +++ b/test-apps/all-files-cwd/second.js @@ -0,0 +1,7 @@ +// this file should be excluded from the final coverage numbers +// using "nyc.exclude" list in package.json +window.reverse = s => + s + .split('') + .reverse() + .join('') From 5efe9310132447816c2310f2c25995fb548c3ebe Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 13:18:14 +0200 Subject: [PATCH 2/7] ci: add test-all-files-cwd to publish requirements Ensure the new test case is included in the CI pipeline before allowing releases to be published. --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index d70ae4bc7..b9ac1ab13 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -175,6 +175,7 @@ workflows: - lint - test-code-coverage-plugin - test-all-files + - test-all-files-cwd - test-backend - test-batch-send-coverage - test-before-all-visit From 22d5663b9b995cc32a5afb4a2c747ce47d4f8071 Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 14:58:47 +0200 Subject: [PATCH 3/7] fix: prettier --- task-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task-utils.js b/task-utils.js index 945b349c2..6ebf1ae68 100644 --- a/task-utils.js +++ b/task-utils.js @@ -336,7 +336,7 @@ function findSourceFiles(nycOptions) { globbyOptions.cwd = nycOptions.cwd } const allFiles = globby.sync(patterns, globbyOptions) - + return allFiles } /** From 0c1cbbb3bf7c3220bdfc84f0cd22b461d0d391f1 Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 15:20:12 +0200 Subject: [PATCH 4/7] fix: update start scripts to reference correct file paths --- test-apps/all-files-cwd/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-apps/all-files-cwd/package.json b/test-apps/all-files-cwd/package.json index dc1b4a5f4..eb8d5c064 100644 --- a/test-apps/all-files-cwd/package.json +++ b/test-apps/all-files-cwd/package.json @@ -4,8 +4,8 @@ "private": true, "scripts": { "cy:run": "cypress run", - "start": "parcel serve index.html", - "start:windows": "npx bin-up parcel serve index.html", + "start": "parcel serve ../all-files/index.html", + "start:windows": "npx bin-up parcel serve ../all-files/index.html", "pretest": "rimraf .nyc_output .cache coverage dist", "test": "start-test 1234 cy:run", "coverage:verify": "npx nyc report --check-coverage true --lines 100", From 4d903622cfbf080bb905560bc7f3652a5d024a01 Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 15:26:05 +0200 Subject: [PATCH 5/7] fix: add temp-dir and report-dir to NYC configuration to set different dir from cwd --- test-apps/all-files-cwd/package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-apps/all-files-cwd/package.json b/test-apps/all-files-cwd/package.json index eb8d5c064..fd1c09b6a 100644 --- a/test-apps/all-files-cwd/package.json +++ b/test-apps/all-files-cwd/package.json @@ -14,6 +14,8 @@ "nyc": { "all": true, "cwd": "../all-files", + "temp-dir": ".nyc_output", + "report-dir": "cypress-coverage", "include": "*.js" }, "devDependencies": { From b0d74cbf4bcc3b0734d960409012a87e224fbee0 Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 15:41:47 +0200 Subject: [PATCH 6/7] fix: update coverage:verify script to specify temp-dir for NYC report cause it uses cwd as default folder also for temp-dir --- test-apps/all-files-cwd/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-apps/all-files-cwd/package.json b/test-apps/all-files-cwd/package.json index fd1c09b6a..c85c2e77b 100644 --- a/test-apps/all-files-cwd/package.json +++ b/test-apps/all-files-cwd/package.json @@ -8,7 +8,7 @@ "start:windows": "npx bin-up parcel serve ../all-files/index.html", "pretest": "rimraf .nyc_output .cache coverage dist", "test": "start-test 1234 cy:run", - "coverage:verify": "npx nyc report --check-coverage true --lines 100", + "coverage:verify": "npx nyc report --temp-dir ../all-files-cwd/.nyc_output --check-coverage true --lines 100", "coverage:check-files": "check-coverage main.js && check-coverage second.js && check-coverage not-covered.js && check-coverage cypress.config.js && only-covered --from coverage/coverage-final.json main.js second.js not-covered.js cypress.config.js" }, "nyc": { From a10f57bca33ca7ac9ff1d725bdbcf47bddeef19b Mon Sep 17 00:00:00 2001 From: alecjs Date: Wed, 20 Aug 2025 15:47:32 +0200 Subject: [PATCH 7/7] restore: remove unnecessary config from nyc --- test-apps/all-files-cwd/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/test-apps/all-files-cwd/package.json b/test-apps/all-files-cwd/package.json index c85c2e77b..b0cc9cdb8 100644 --- a/test-apps/all-files-cwd/package.json +++ b/test-apps/all-files-cwd/package.json @@ -14,8 +14,6 @@ "nyc": { "all": true, "cwd": "../all-files", - "temp-dir": ".nyc_output", - "report-dir": "cypress-coverage", "include": "*.js" }, "devDependencies": {