Wener Notes

Ava

约 2 分钟阅读
  • avajs/avaGitHubavajs/avaavajs/ava · 小、快、简单 · process.env.NODEENV=test · nodemodules/.cache/ava · React Recipe · 配合 enzyme 测试 react · pnpm 下 ava 为 shell, 实际脚本为笔记:Ava
    • 小、快、简单
  • process.env.NODE_ENV=test
  • node_modules/.cache/ava
  • React RecipeGitHub Fileavajs/ava/docs/recipes/react.md
    • 配合 enzyme 测试 react
  • pnpm 下 ava 为 shell, 实际脚本为 node_modules/ava/entrypoints/cli.mjs
    • 如果使用了 WebStorm 插件可以修改 ava 路径
    • 将 ava 安装到 root 可避免
packages.json
{
  "devDependencies": {
    "ava": "*",
    "tsx": "*"
  },
  "ava": {
    "extensions": {
      "ts": "module"
    },
    "nodeArguments": ["--conditions=typescript", "--loader=tsx"]
  }
}
TypeScript
import test from 'ava';

test('foo', (t) => {
  t.pass();
});

test('bar', async (t) => {
  const bar = Promise.resolve('bar');
  t.is(await bar, 'bar');
});

// 串行执行
test.serial('passes serially', (t) => {
  t.pass();
});

test.only('will be run', (t) => {
  t.pass();
});
test.skip('will not be run', (t) => {
  t.fail();
});

test.todo('later');
test.serial.todo('later');

// 标记失败测试
test.failing('demonstrate some bug', (t) => {
  t.fail(); // Test will count as passed
});

// 增加输出内容
import util from 'util';
util.inspect.defaultOptions.depth = 5;
  • test.{before,after,beforeEach,afterEach}
  • test.after.always
  • test.afterEach.always
  • t.context - 测试上下文
  • test.meta.file - 元信息
bash
npx @ava/init

npx ava
npx ava src/hello.test.ts
# match by title
npx ava src/hello.test.ts -m "db"

npx ava --watch
DEBUG=ava:watcher npx ava --watch

# tap report
npx ava --tap | npx tap-nyan

测试文件匹配逻辑

  • 以 _ 开头的目录和文件会忽略

默认测试文件匹配逻辑

text
test.js
src/test.js
source/test.js
**/test-*.js
**/*.spec.js
**/*.test.js
**/test/**/*.js
**/tests/**/*.js
**/__tests__/**/*.js

排除逻辑

text
**/__tests__/**/__helper__/**/*
**/__tests__/**/__helpers__/**/*
**/__tests__/**/__fixture__/**/*
**/__tests__/**/__fixtures__/**/*
**/test/**/helper/**/*
**/test/**/helpers/**/*
**/test/**/fixture/**/*
**/test/**/fixtures/**/*
**/tests/**/helper/**/*
**/tests/**/helpers/**/*
**/tests/**/fixture/**/*
**/tests/**/fixtures/**/*

配置

json
{
  "ava": {
    "files": ["test/**/*", "!test/exclude-files-in-this-directory", "!**/exclude-files-with-this-name.*"],
    "ignoredByWatcher": "",
    // --match
    // "match": ["*oo", "!foo"],
    // node_modules/.cache/ava
    "cache": true,
    // nproc
    "concurrency": 5,
    "workerThreads": true,
    "failFast": true,
    // 没有 assert 是否认为失败
    "failWithoutAssertions": false,
    "environmentVariables": {
      "MY_ENVIRONMENT_VARIABLE": "some value"
    },
    "tap": false,
    "verbose": true,
    // "snapshotDir":"",
    "extensions": ["cjs", "mjs", "js"],
    "require": ["./my-helper-module.js"],
    "timeout": 0,
    "nodeArguments": ["--trace-deprecation", "--napi-modules"],
    // ava.config.*  使用,定义函数
    // sortTestFiles:"",
    "utilizeParallelBuilds": true
  }
}

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • Web Dev FAQ
    笔记 · Ava

    | abbr. | stand for | · | iife | Immediately Invoked Function Expression | · 在开发时 assert 错误 · invariant · 早期来自 React, flux

References

GitHub6 条
其他外链1 条
最近更新commit 906e816Edit

On this page