如何使用jest config全局导入模块?

时间:2017-08-15 22:08:04

标签: chai jest

我有一个测试文件,如: let { expect } = chai; describe(...) .... 当我运行jest时,我的测试显然失败,错误chai未定义。简单的解决方案是在每个测试文件中导入chai,但我更愿意找到更好的解决方案。有没有办法在jestConfig中全局导入chai?

我也尝试过setupFiles配置来运行一个导入了chai的文件,但是没有用。

1 个答案:

答案 0 :(得分:0)

Jest setupFiles配置将完成此工作。将chai模块附加到global对象,然后就可以在测试文件中直接使用chai

例如

jest.config.js

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'enzyme',
  setupFiles: [
    '/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/45702292/chai.setup.js'
  ],
  setupFilesAfterEnv: ['jest-enzyme'],
  testEnvironmentOptions: {
    enzymeAdapter: 'react16',
  },
  coverageReporters: ['json', 'text', 'lcov', 'clover'],
  testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
};

chai.setup.js

global.chai = require('chai')

index.test.js

describe('45702292', () => {
  it('should pass', () => {
    console.log(chai)
    chai.expect(1+1).to.be.eq(2)
  });
});

测试结果:

 PASS  src/stackoverflow/45702292/index.test.js (16.492s)
  45702292
    ✓ should pass (18ms)

  console.log src/stackoverflow/45702292/index.test.js:3
    { version: '4.2.0',
      AssertionError: [Function: AssertionError],
      use: [Function],
      util: 1,
      config:
       { includeStack: false,
         showDiff: true,
         truncateThreshold: 40,
         useProxy: true,
         proxyExcludedKeys: [ 'then', 'catch', 'inspect', 'toJSON' ] },
      Assertion:
       { [Function: Assertion]
         addProperty: [Function],
         addMethod: [Function],
         addChainableMethod: [Function],
         overwriteProperty: [Function],
         overwriteMethod: [Function],
         overwriteChainableMethod: [Function] },
      expect: { [Function] fail: [Function] },
      should: [Function: loadShould],
      Should: [Function: loadShould],
      assert:
       { [Function]
         fail: [Function],
         isOk: [Function],
         isNotOk: [Function],
         equal: [Function],
         notEqual: [Function],
         strictEqual: [Function],
         notStrictEqual: [Function],
         deepStrictEqual: [Function],
         deepEqual: [Function],
         notDeepEqual: [Function],
         isAbove: [Function],
         isAtLeast: [Function],
         isBelow: [Function],
         isAtMost: [Function],
         isTrue: [Function],
         isNotTrue: [Function],
         isFalse: [Function],
         isNotFalse: [Function],
         isNull: [Function],
         isNotNull: [Function],
         isNaN: [Function],
         isNotNaN: [Function],
         exists: [Function],
         notExists: [Function],
         isUndefined: [Function],
         isDefined: [Function],
         isFunction: [Function],
         isNotFunction: [Function],
         isObject: [Function],
         isNotObject: [Function],
         isArray: [Function],
         isNotArray: [Function],
         isString: [Function],
         isNotString: [Function],
         isNumber: [Function],
         isNotNumber: [Function],
         isFinite: [Function],
         isBoolean: [Function],
         isNotBoolean: [Function],
         typeOf: [Function],
         notTypeOf: [Function],
         instanceOf: [Function],
         notInstanceOf: [Function],
         include: [Function],
         notInclude: [Function],
         deepInclude: [Function],
         notDeepInclude: [Function],
         nestedInclude: [Function],
         notNestedInclude: [Function],
         deepNestedInclude: [Function],
         notDeepNestedInclude: [Function],
         ownInclude: [Function],
         notOwnInclude: [Function],
         deepOwnInclude: [Function],
         notDeepOwnInclude: [Function],
         match: [Function],
         notMatch: [Function],
         property: [Function],
         notProperty: [Function],
         propertyVal: [Function],
         notPropertyVal: [Function],
         deepPropertyVal: [Function],
         notDeepPropertyVal: [Function],
         ownProperty: [Function],
         notOwnProperty: [Function],
         ownPropertyVal: [Function],
         notOwnPropertyVal: [Function],
         deepOwnPropertyVal: [Function],
         notDeepOwnPropertyVal: [Function],
         nestedProperty: [Function],
         notNestedProperty: [Function],
         nestedPropertyVal: [Function],
         notNestedPropertyVal: [Function],
         deepNestedPropertyVal: [Function],
         notDeepNestedPropertyVal: [Function],
         lengthOf: [Function],
         hasAnyKeys: [Function],
         hasAllKeys: [Function],
         containsAllKeys: [Function],
         doesNotHaveAnyKeys: [Function],
         doesNotHaveAllKeys: [Function],
         hasAnyDeepKeys: [Function],
         hasAllDeepKeys: [Function],
         containsAllDeepKeys: [Function],
         doesNotHaveAnyDeepKeys: [Function],
         doesNotHaveAllDeepKeys: [Function],
         throws: [Function],
         doesNotThrow: [Function],
         operator: [Function],
         closeTo: [Function],
         approximately: [Function],
         sameMembers: [Function],
         notSameMembers: [Function],
         sameDeepMembers: [Function],
         notSameDeepMembers: [Function],
         sameOrderedMembers: [Function],
         notSameOrderedMembers: [Function],
         sameDeepOrderedMembers: [Function],
         notSameDeepOrderedMembers: [Function],
         includeMembers: [Function],
         notIncludeMembers: [Function],
         includeDeepMembers: [Function],
         notIncludeDeepMembers: [Function],
         includeOrderedMembers: [Function],
         notIncludeOrderedMembers: [Function],
         includeDeepOrderedMembers: [Function],
         notIncludeDeepOrderedMembers: [Function],
         oneOf: [Function],
         changes: [Function],
         changesBy: [Function],
         doesNotChange: [Function],
         changesButNotBy: [Function],
         increases: [Function],
         increasesBy: [Function],
         doesNotIncrease: [Function],
         increasesButNotBy: [Function],
         decreases: [Function],
         decreasesBy: [Function],
         doesNotDecrease: [Function],
         doesNotDecreaseBy: [Function],
         decreasesButNotBy: [Function],
         ifError: [Function],
         isExtensible: [Function],
         isNotExtensible: [Function],
         isSealed: [Function],
         isNotSealed: [Function],
         isFrozen: [Function],
         isNotFrozen: [Function],
         isEmpty: [Function],
         isNotEmpty: [Function],
         ok: [Function],
         notOk: [Function],
         throw: [Function],
         Throw: [Function],
         extensible: [Function],
         notExtensible: [Function],
         sealed: [Function],
         notSealed: [Function],
         frozen: [Function],
         notFrozen: [Function],
         empty: [Function],
         notEmpty: [Function] } }

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        18.971s