-
Notifications
You must be signed in to change notification settings - Fork 55
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
📦 NEW: Support run test with node:test #177
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
"extends": "eslint-config-egg", | ||
"rules": { | ||
/** | ||
* @see http://eslint.org/docs/rules/strict | ||
*/ | ||
"strict": [ "off" ] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
'use strict'; | ||
|
||
const debug = require('debug')('egg-bin'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const globby = require('globby'); | ||
const Command = require('../command'); | ||
const { getChangedTestFiles } = require('../utils'); | ||
|
||
class NodeTestCommand extends Command { | ||
constructor(rawArgv) { | ||
super(rawArgv); | ||
this.usage = 'Usage: egg-bin node-test [files] [options]'; | ||
this.options = { | ||
require: { | ||
description: 'require the given module', | ||
alias: 'r', | ||
type: 'array', | ||
}, | ||
grep: { | ||
description: 'only run tests matching <pattern>', | ||
alias: 'g', | ||
type: 'array', | ||
}, | ||
timeout: { | ||
description: 'set test-case timeout in milliseconds', | ||
alias: 't', | ||
type: 'number', | ||
}, | ||
changed: { | ||
description: 'only test with changed files and match ${cwd}/test/**/*.test.(js|ts)', | ||
alias: 'c', | ||
}, | ||
}; | ||
} | ||
|
||
get description() { | ||
return 'Run test with node:test'; | ||
} | ||
|
||
async run(context) { | ||
const opt = { | ||
env: Object.assign({ | ||
NODE_ENV: 'test', | ||
}, context.env), | ||
execArgv: context.execArgv, | ||
}; | ||
const testArgs = await this.formatTestArgs(context); | ||
if (!testArgs) return; | ||
|
||
if (parseInt(process.version.split('.')[0].substring(1)) < 18) { | ||
// using user land test module | ||
// https://github.com/nodejs/node-core-test | ||
const nodeTestBin = require.resolve('test/bin/node--test'); | ||
debug('run test: %s %s', nodeTestBin, testArgs.join(' ')); | ||
await this.helper.forkNode(nodeTestBin, testArgs, opt); | ||
return; | ||
} | ||
testArgs.unshift('--test'); | ||
debug('run test: %s %s', process.execPath, testArgs.join(' ')); | ||
await this.helper.spawn(process.execPath, testArgs, opt); | ||
} | ||
|
||
/** | ||
* format test args then change it to array style | ||
* @param {Object} context - { cwd, argv, ...} | ||
* @param context.argv | ||
* @return {Array} [ '--require=xxx', 'xx.test.js' ] | ||
* @protected | ||
*/ | ||
async formatTestArgs({ argv }) { | ||
const testArgv = Object.assign({}, argv); | ||
|
||
/* istanbul ignore next */ | ||
// testArgv.timeout = testArgv.timeout || process.env.TEST_TIMEOUT || 60000; | ||
// testArgv.reporter = testArgv.reporter || process.env.TEST_REPORTER; | ||
|
||
// collect require | ||
let requireArr = testArgv.require || testArgv.r || []; | ||
/* istanbul ignore next */ | ||
if (!Array.isArray(requireArr)) requireArr = [ requireArr ]; | ||
|
||
testArgv.require = requireArr; | ||
|
||
let pattern; | ||
// changed | ||
if (testArgv.changed) { | ||
pattern = await this._getChangedTestFiles(); | ||
if (!pattern.length) { | ||
console.log('No changed test files'); | ||
return; | ||
} | ||
} | ||
|
||
if (!pattern) { | ||
// specific test files | ||
pattern = testArgv._.slice(); | ||
} | ||
if (!pattern.length && process.env.TESTS) { | ||
pattern = process.env.TESTS.split(','); | ||
} | ||
|
||
// collect test files | ||
if (!pattern.length) { | ||
pattern = [ `test/**/*.test.${testArgv.typescript ? 'ts' : 'js'}` ]; | ||
} | ||
pattern = pattern.concat([ '!test/fixtures', '!test/node_modules' ]); | ||
|
||
// expand glob and skip node_modules and fixtures | ||
const files = globby.sync(pattern); | ||
files.sort(); | ||
|
||
if (files.length === 0) { | ||
console.log(`No test files found with ${pattern}`); | ||
return; | ||
} | ||
|
||
// auto add setup file as the first test file | ||
const setupFile = path.join(process.cwd(), `test/.setup.${testArgv.typescript ? 'ts' : 'js'}`); | ||
if (fs.existsSync(setupFile)) { | ||
files.unshift(setupFile); | ||
} | ||
testArgv._ = files; | ||
|
||
// remove alias | ||
testArgv.$0 = undefined; | ||
testArgv.r = undefined; | ||
testArgv.t = undefined; | ||
testArgv.g = undefined; | ||
testArgv.e = undefined; | ||
testArgv['dry-run'] = undefined; | ||
testArgv.dryRun = undefined; | ||
// not support tscompiler for now | ||
testArgv.tscompiler = undefined; | ||
|
||
return this.helper.unparseArgv(testArgv); | ||
} | ||
|
||
async _getChangedTestFiles() { | ||
return await getChangedTestFiles(process.cwd()); | ||
} | ||
} | ||
|
||
module.exports = NodeTestCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const path = require('path'); | ||
const changed = require('jest-changed-files'); | ||
|
||
module.exports = { | ||
async getChangedTestFiles(dir) { | ||
const res = await changed.getChangedFilesForRoots([ dir ]); | ||
const changedFiles = res.changedFiles; | ||
const files = []; | ||
for (const file of changedFiles) { | ||
// only find ${dir}/test/**/*.test.(js|ts) | ||
if (file.startsWith(path.join(dir, 'test')) && file.match(/\.test\.(js|ts)$/)) { | ||
files.push(file); | ||
} | ||
} | ||
return files; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait for eggjs/eslint-config-egg#64