项目作者: unional

项目描述 :
Provide fixture to tests
高级语言: TypeScript
项目地址: git://github.com/unional/fixture.git
创建时间: 2018-02-28T09:21:17Z
项目社区:https://github.com/unional/fixture

开源协议:MIT License

下载


@unional/fixture

@unional/fixture"">NPM version
@unional/fixture"">NPM downloads

GitHub NodeJS
Codecov

Semantic Release

Visual Studio Code

Provides fixture for tests.
You can use it for any test runner such as jest, ava, mocha, tap, etc.

Install

  1. # npm
  2. npm install -D @unional/fixture
  3. # yarn
  4. yarn add -D @unional/fixture
  5. # pnpm
  6. pnpm install -D @unional/fixture
  7. # rush
  8. rush add -p @unional/fixture --dev

Usage

baseline() is the heart of @unional/fixture.
You can use it to read a folder containing multiple test cases and use them in your test runner.

  1. import { baseline } from '@unional/fixture'
  2. // basic usage
  3. baseline('fixtures', ({ caseName, caseType, casePath, resultPath, match, copyToBaseline }) => {
  4. // `test()` comes from your favorite test runner.
  5. // e.g. `ava`, `jest`, `mocha`
  6. test(caseName, async () => {
  7. // this example assumes `caseType === 'file'`,
  8. // so the `casePath` points to the input file directly.
  9. fs.readFileSync(casePath, 'utf-8')
  10. // `resultPath` points to a folder where you can save your result(s)
  11. fs.writeFileSync(path.join(resultPath, 'output.txt', '<some data>'))
  12. // compare result and baseline folder
  13. await match()
  14. // match compares specific file in result folder and baseline folder
  15. await match('output.txt')
  16. // if you are happy with the change,
  17. // use this to copy the artifacts from result folder to baseline folder,
  18. // or you can do that manually from your IDE.
  19. await copyToBaseline('*.txt')
  20. })
  21. // advance usage
  22. baseline({
  23. basePath: 'fixtures',
  24. // default: 'cases'
  25. casesFolder: 'scenarios',
  26. // default: 'results'
  27. resultsFolder: 'actuals',
  28. // default: 'baselines'
  29. baselinesFolder: 'expects',
  30. // filter for specific cases
  31. // can use wildcards or RegExp
  32. filter: '*.pass',
  33. // By default warning messages will be displayed when some test cases are filtered.
  34. // Use this to suppress those warnings.
  35. suppressFilterWarnings: true,
  36. // If the file has more lines than this threshold,
  37. // then the file will be diff with line numbers,
  38. // and the unchanged lines will be trimmed off.
  39. largeFileThreshold: 100,
  40. // Controls how many unchanged lines will be shown around the changes.
  41. largeFileAmbientLines: 5,
  42. /**
  43. * Maximum number of diff lines to show.
  44. * If there are more diff lines,
  45. * the remaining will be timmed and show a summary instead.
  46. */
  47. diffDisplayThreshold: 150
  48. }, (context) => {
  49. ...
  50. })
  51. })

Command Testing

In addition to baseline(),
@unional/fixture provides execCommand() and writeCommandResult() to run command line test within the fixture.

If the test case is a JSON or YAML, it will read and execute the command within.
If the test case is a folder, it will look for a command.json|yml|yaml in the folder and does the same.

More file types and features will be added in the future.

Here is a common way to use them:

  1. import { baseline, execCommand, writeCommandResult } from '@unional/fixture'
  2. baseline('fixtures/command', ({ caseType, caseName, casePath, resultPath, match }) => {
  3. it(caseName, async () => {
  4. writeCommandResult(
  5. resultPath,
  6. await execCommand({ caseType, caseName, casePath })
  7. )
  8. return match()
  9. })
  10. })

The execCommand() has an alternative signature that allows you to easily run and capture stdout and stderr:

  1. const { stdout, stderr } = await execCommand({ casePath, command, args })

Contribute

  1. # right after fork
  2. yarn
  3. # begin making changes
  4. git checkout -b <branch>
  5. yarn watch
  6. # edit `webpack.config.dev.js` to exclude dependencies for the global build.
  7. # after making change(s)
  8. git commit -m "<commit message>"
  9. git push
  10. # create PR

Commands

There are a few useful commands you can use during development.

  1. # Run tests (and lint) automatically whenever you save a file.
  2. yarn watch
  3. # Run tests with coverage stats (but won't fail you if coverage does not meet criteria)
  4. yarn test
  5. # Manually verify the project.
  6. # This will be ran during 'npm preversion' so you normally don't need to run this yourself.
  7. yarn verify
  8. # Build the project.
  9. # You normally don't need to do this.
  10. yarn build
  11. # Run tslint
  12. # You normally don't need to do this as `yarn watch` and `npm version` will automatically run lint for you.
  13. yarn lint