Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try reproducing incorrect coverage when loading querystring #501

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"scripts": {
"test": "cross-env TS_NODE_SKIP_PROJECT=true node ./bin/c8.js mocha --timeout=10000 ./test/*.js",
"test-loader": "cross-env TS_NODE_SKIP_PROJECT=true node ./bin/c8.js mocha --timeout=10000 ./test/source-resolved-from-loader.js",
"coverage": "cross-env TS_NODE_SKIP_PROJECT=true node ./bin/c8.js --check-coverage mocha --timeout=10000 ./test/*.js",
"test:snap": "cross-env CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test",
"fix": "standard --fix",
Expand Down
38 changes: 38 additions & 0 deletions test/fixtures/all/vanilla/main.querystring-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import module from 'node:module'

async function resolve (moduleId, context, next) {
const result = await next(moduleId, context);
const url = new URL(result.url)
url.searchParams.set('randomSeed', Math.random());
result.url = url.href;
return result;
}

function load(url, context, next) {
if (url.includes('main.js')) {
const loadedId = url.replace('main.js', 'loaded.js')

return {
shortCircuit: true,
format: 'module',
source: `import loaded from "${loadedId}";loaded()`,
};
}

if (url.includes('loaded.js')) {
return {
shortCircuit: true,
format: 'module',
source: 'export default () => true',
};
}

return next(url, context);
}

module.register && module.register(`
data:text/javascript,
export ${encodeURIComponent(load)}
export ${encodeURIComponent(resolve)}`.slice(1))

export default import('./main.js?qs=1')
50 changes: 50 additions & 0 deletions test/source-resolved-from-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* global describe, before, beforeEach, it */

const { spawnSync } = require('child_process')
const c8Path = require.resolve('../bin/c8')
const nodePath = process.execPath
const tsNodePath = './node_modules/.bin/ts-node'
const chaiJestSnapshot = require('chai-jest-snapshot')
const rimraf = require('rimraf')
const nodemodule = require('node:module')

require('chai')
.use(chaiJestSnapshot)
.should()

before(cb => rimraf('tmp', cb))

beforeEach(function () {
chaiJestSnapshot.configureUsingMochaContext(this)
})

;[false, true].forEach((mergeAsync) => {
const title = mergeAsync ? 'c8+loader mergeAsync' : 'c8+loader'
describe(title, () => {
if (nodemodule.register) {
it('reports coverage for query-loaded js files, with accuracy', async () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/vanilla-all',
'--clean=false',
'--all=true',
'--include=test/fixtures/all/vanilla/**/*.js',
'--exclude=**/*.ts', // add an exclude to avoid default excludes of test/**
`--merge-async=${mergeAsync}`,
nodePath,
'--experimental-default-type=module',
require.resolve('./fixtures/all/vanilla/main.querystring-import.mjs')
])

// test fails if loaded row looks like this
// loaded.js | 100 | 100 | 100 | 100 |
output.toString('utf8')
.split('\n')
.filter(l => l.includes(' loaded.js'))[0]
.split('|').filter(l => /\d/.test(l))
.every(l => !l.includes(100))
.should.equal(true)
})
}
})
})