根据环境禁用 Jasmine 的 fdescribe() 和 fit()
Disable Jasmine's fdescribe() and fit() based on environment
fdescribe()
和 fit()
非常适合在处理测试子集时减少噪音。在将我的分支合并到 master 之前,我有时会忘记将它们改回 describe()
/it()
。 (在处理代码时将它们放在单独的分支中是可以的 - 即提交前检查对我不起作用。)
我的 CI 环境是 Codeship。是否有解决此问题的解决方案,如果遇到任何聚焦方法,Codeship 中的测试将失败?
使用 no-focused-tests 之类的东西就可以了。知道如何在 Codeship 中将此规则作为错误启用并在本地禁用吗?
Using something like no-focused-tests would be okay. Any idea how to enable this rule as an error in Codeship and disable it locally?
您可以结合使用环境变量并重新定义 fdescribe/fit 全局函数:
npm i --save cross-env
package.json:
"scripts": {
"test": "jasmine",
"test-safe": "cross-env FOCUSED_TESTS=off jasmine"
},
disableFocusedTestsIfNecessary.js(包含在之后 jasmine 定义了它的全局变量):
if (process.env.FOCUSED_TESTS === "off") {
console.log("Focused tests must be off");
global.fdescribe = global.fit = function() {
throw new Error("fdescribe and fit are disabled in this environment");
};
}
else {
console.log("Focused tests enabled");
}
告诉 codeship 到 运行 npm run test-safe
而不是 npm run test
对于那些感兴趣的人,如果你正在使用 jasmine 和 eslint,你可以使用这个插件来确保没有重点测试:https://github.com/tlvince/eslint-plugin-jasmine.
- 首先全局安装eslint
npm install -g eslint
。
- 然后安装eslint-plugin-jasmine库
npm install --save-dev eslint-plugin-jasmine
。
创建一个看起来像这样的 .eslintrc
文件:
{
"rules": {
"semi": 2
},
"plugins": ["jasmine"],
"env": {
"jasmine": true
},
"extends": "plugin:jasmine/recommended",
}
然后你就可以运行 linter eslint -c ./.eslintrc app.js
编辑 14.11.19:
为了方便起见,我创建了一个可安装包,您可以在 https://www.npmjs.com/package/tslint-jasmine
找到
原文post:
如果您正在使用 TSLint 并且(像我一样)发现所有 defocus 和 tslint-jasmine-noSkipOrFocus 检查器都不适合您,我为此创建了一个要点:https://gist.github.com/djungowski/7d9126bb79970446b4ffeb5656c6bf1f
使用方法:
- 将 Gist 保存在名为
TSLint/Rules
的文件夹中,作为 noJasmineFocusRule.js
- 将规则文件夹添加到您的 TSLint 配置中:
rulesDirectory: 'TSLint/Rules'
- 使用
"no-jasmine-focus": true
启用选项
这不是最佳解决方案。但它适合我的需要。
要设置:
npm i lodash
npm i minimist
我从我的 gulp 任务中调用它:
node .\build\throwIfFocusedTest.js e2e/
node .\build\throwIfFocusedTest.js src/
throwIfFocusedTest.js:
const walkSync = require('./walkSync').default;
const _ = require('lodash');
const argv = require('minimist')(process.argv);
const fs = require('fs');
if (argv._.length !== 3) {
throw 'expecting 1 command line argument';
}
const directory = argv._[2];
const files = walkSync(directory);
const scriptFiles = _.filter(files, f => f.endsWith('.js') || f.endsWith('.ts'));
const invalidStrings = [
'fdescribe',
'fit',
];
_.each(scriptFiles, fileName => {
const contents = fs.readFileSync(fileName, 'utf8');
invalidStrings.forEach(is => {
if (contents.includes(is)) {
console.error(`throwIfFocusedTest: ${directory}: File contains ${is}: ${fileName}`);
process.exit(1);
}
});
});
console.log(`throwIfFocusedTest: ${directory}: No files contain: ${invalidStrings.join(', ')}`);
walkSync.js:
/**
* From: https://gist.github.com/kethinov/6658166
*/
exports.default = function walkSync(dir, filelist) {
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
var path = dir + file;
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(path);
}
});
return filelist;
};
我迟到了。
我的构建也有类似的问题。我们不使用 ts / eslint,所以我只是写了一个快速脚本来抛出一个错误,该错误会使我的 dockerfile / build 失败。
在这里。
#!/bin/sh
files=$(find "./.." -type f -name '*.spec*')
errored=false
echo "Checking for focused tests"
for file in $files
do
if grep -E "fdescribe|fit" $file; [ $? -eq 0 ]; then
echo "-Focusing a test in the file $file"
errored=true
fi
done
if $errored; then
echo "Some tests were focused"
exit 1
else
echo "No tests were focused"
fi
如果你愿意在测试标记为焦点时失败 或 跳过 (fit
+ xit
),有一个相对较新的 Karma无需插件即可解决问题的功能。 Karma 现在支持 failOnSkippedTests
配置文件/CLI 选项,根据 the docs,这会导致“故意禁用的测试失败,例如 fit() 或 xit()”。
fdescribe()
和 fit()
非常适合在处理测试子集时减少噪音。在将我的分支合并到 master 之前,我有时会忘记将它们改回 describe()
/it()
。 (在处理代码时将它们放在单独的分支中是可以的 - 即提交前检查对我不起作用。)
我的 CI 环境是 Codeship。是否有解决此问题的解决方案,如果遇到任何聚焦方法,Codeship 中的测试将失败?
使用 no-focused-tests 之类的东西就可以了。知道如何在 Codeship 中将此规则作为错误启用并在本地禁用吗?
Using something like no-focused-tests would be okay. Any idea how to enable this rule as an error in Codeship and disable it locally?
您可以结合使用环境变量并重新定义 fdescribe/fit 全局函数:
npm i --save cross-env
package.json:
"scripts": { "test": "jasmine", "test-safe": "cross-env FOCUSED_TESTS=off jasmine" },
disableFocusedTestsIfNecessary.js(包含在之后 jasmine 定义了它的全局变量):
if (process.env.FOCUSED_TESTS === "off") { console.log("Focused tests must be off"); global.fdescribe = global.fit = function() { throw new Error("fdescribe and fit are disabled in this environment"); }; } else { console.log("Focused tests enabled"); }
告诉 codeship 到 运行
npm run test-safe
而不是npm run test
对于那些感兴趣的人,如果你正在使用 jasmine 和 eslint,你可以使用这个插件来确保没有重点测试:https://github.com/tlvince/eslint-plugin-jasmine.
- 首先全局安装eslint
npm install -g eslint
。 - 然后安装eslint-plugin-jasmine库
npm install --save-dev eslint-plugin-jasmine
。 创建一个看起来像这样的
.eslintrc
文件:{ "rules": { "semi": 2 }, "plugins": ["jasmine"], "env": { "jasmine": true }, "extends": "plugin:jasmine/recommended", }
然后你就可以运行 linter
eslint -c ./.eslintrc app.js
编辑 14.11.19:
为了方便起见,我创建了一个可安装包,您可以在 https://www.npmjs.com/package/tslint-jasmine
找到原文post:
如果您正在使用 TSLint 并且(像我一样)发现所有 defocus 和 tslint-jasmine-noSkipOrFocus 检查器都不适合您,我为此创建了一个要点:https://gist.github.com/djungowski/7d9126bb79970446b4ffeb5656c6bf1f
使用方法:
- 将 Gist 保存在名为
TSLint/Rules
的文件夹中,作为noJasmineFocusRule.js
- 将规则文件夹添加到您的 TSLint 配置中:
rulesDirectory: 'TSLint/Rules'
- 使用
"no-jasmine-focus": true
启用选项
这不是最佳解决方案。但它适合我的需要。
要设置:
npm i lodash
npm i minimist
我从我的 gulp 任务中调用它:
node .\build\throwIfFocusedTest.js e2e/
node .\build\throwIfFocusedTest.js src/
throwIfFocusedTest.js:
const walkSync = require('./walkSync').default;
const _ = require('lodash');
const argv = require('minimist')(process.argv);
const fs = require('fs');
if (argv._.length !== 3) {
throw 'expecting 1 command line argument';
}
const directory = argv._[2];
const files = walkSync(directory);
const scriptFiles = _.filter(files, f => f.endsWith('.js') || f.endsWith('.ts'));
const invalidStrings = [
'fdescribe',
'fit',
];
_.each(scriptFiles, fileName => {
const contents = fs.readFileSync(fileName, 'utf8');
invalidStrings.forEach(is => {
if (contents.includes(is)) {
console.error(`throwIfFocusedTest: ${directory}: File contains ${is}: ${fileName}`);
process.exit(1);
}
});
});
console.log(`throwIfFocusedTest: ${directory}: No files contain: ${invalidStrings.join(', ')}`);
walkSync.js:
/**
* From: https://gist.github.com/kethinov/6658166
*/
exports.default = function walkSync(dir, filelist) {
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
var path = dir + file;
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(path);
}
});
return filelist;
};
我迟到了。
我的构建也有类似的问题。我们不使用 ts / eslint,所以我只是写了一个快速脚本来抛出一个错误,该错误会使我的 dockerfile / build 失败。
在这里。
#!/bin/sh
files=$(find "./.." -type f -name '*.spec*')
errored=false
echo "Checking for focused tests"
for file in $files
do
if grep -E "fdescribe|fit" $file; [ $? -eq 0 ]; then
echo "-Focusing a test in the file $file"
errored=true
fi
done
if $errored; then
echo "Some tests were focused"
exit 1
else
echo "No tests were focused"
fi
如果你愿意在测试标记为焦点时失败 或 跳过 (fit
+ xit
),有一个相对较新的 Karma无需插件即可解决问题的功能。 Karma 现在支持 failOnSkippedTests
配置文件/CLI 选项,根据 the docs,这会导致“故意禁用的测试失败,例如 fit() 或 xit()”。