
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
yhtml5-test
Advanced tools
A test framework for front-end projects
目录
目的
运行代码没有错误
找到并删除无用的历史代码
寻找隐藏的bug
模拟各种输入, 运行查看输出是否错误
why unit testing
测试框架解决的问题:
命令行
scripts | description |
---|---|
npm test | run test |
npm test src/app | only testing src/app derictory |
npm run test:c | generating coverage reportes |
npm run test:u | update test framework |
匹配器
断言库
覆盖率报告
快照
组合测试
"test": "2dfire-scripts test --env=jsdom",
"test:c": "npm test -- --coverage",
"test:u": "npm i @2dfire/2dfire-scripts@latest -D",
run
npm i @2dfire/2dfire-scripts@latest -D
npm i react-test-renderer@15.6.x enzyme@2.9.x jest-enzyme@3.8.x -D
const path = require('path')
const webpackConfigAlias = {
'^src(.*)$': path.resolve(__dirname, 'src$1'),
}
const config = {
test: {
testMatch: ['demo/__test__/**/*.js?(x)'],
transformIgnorePatterns: ["node_modules/(?!(yhtml5-test|react-redux|react-native-button)/)"],
moduleNameMapper: webpackConfigAlias,
collectCoverageFrom: ['src/**/*.{js,jsx}'],
}
}
module.exports = config
默认配置, 内置在测试框架中
const config = {
collectCoverageFrom: ['src/**/*.{js,jsx}'],
setupFiles: [resolve('utils/polyfills.js')],
setupTestFrameworkScriptFile: setupTestsFile,
testMatch: [
`${rootDir}/src/**/__tests__/**/*.js?(x)`,
`${rootDir}/src/**/?(*.)(spec|test).js?(x)`,
],
testEnvironment: 'node',
testURL: 'http://localhost',
transform: {
'^.+\\.(js|jsx)$': isEjecting
? `${rootDir}/node_modules/babel-jest`
: resolve('utils/babelTransform.js'),
'^.+\\.css$': resolve('utils/cssTransform.js'),
'^(?!.*\\.(js|jsx|css|json)$)': resolve('utils/fileTransform.js'),
},
transformIgnorePatterns:['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
moduleNameMapper: {
'^react-native$': 'react-native-web',
},
moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
};
path.module.function + description
demo
__tests__
directory into module directory which you want to test, like src/welcome/__test__
.js
suffix in __tests__
folders..test.js
suffix..spec.js
suffix.We recommend to put the test files (or tests folders) next to the code they are testing so that relative imports appear shorter. For example, if App.test.js and App.js are in the same folder, the test just needs to import App from './App' instead of a long relative path. Colocation also helps find tests more quickly in larger projects.
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a src/setupTests.js to your project. It will be automatically executed before running your tests.
add setupTests.js into testMatch directory, like src/setupTests.js
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
clear: jest.fn()
};
global.localStorage = localStorageMock
如何书写测试用例
rootDir/packages/yhtml5-test/demo/__test__/
If you’d like to test components in isolation from the child components they render, we recommend using shallow() rendering API from Enzyme.
It is too slow to use when necessary, because
iterate() will run the function several times, create a heap snapshot and repeat that process until there is a set of heap diffs. If a memory leak has been detected an error with some debugging information will be thrown.
You can replace it() with xit() to temporarily exclude a test from being executed. Similarly, fit() lets you focus on a specific test without running any other tests.
for more information, you can read :
语句覆盖率, 条件覆盖率, 行数覆盖率, 函数覆盖率
npm i serve -g
serve -p 10001 ./coverage/lcov-report
.config.js
const config = {
test: {
moduleNameMapper: {'^base(.*)$': path.resolve(__dirname, 'src/base$1'),}
}
}
By default jest doesn't transform ES6 js code from node_modules solution
solution: .config.js add transformIgnorePatterns: ["node_modules/(?!(yhtml5-test|my-project|react-native-button)/)"]
solution: Try importing React in the Components file.
If you are using a React below version 15.5.0, you will also need to install react-addons-test-utils. if you use react@^15.4.2, pleasr run npm i react-addons-test-utils@^15.4.2
FAQs
A test framework for front-end projects
The npm package yhtml5-test receives a total of 64 weekly downloads. As such, yhtml5-test popularity was classified as not popular.
We found that yhtml5-test demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.