如何导入配置文件,以便在Jest测试中可以通过内置的捆绑包和模块找到配置文件?

时间:2019-04-09 11:22:00

标签: javascript node.js typescript jestjs ts-jest

我有一个用TypeScript编写的Node项目,该项目使用Rollup作为捆绑程序,并使用Jest作为单元测试框架。它具有以下结构(我忽略了我认为与该问题无关的内容):

__tests__
   greeter.test.ts
   config.json
config
   template.config.json
src
   modules
      greeter.ts
   index.ts
jest.config.ts
package.json
tsconfig.json

src / index.ts:

import { greeter } from './modules/greeter'
console.log(greeter('Bojan'))

src / modules / greeter.ts:

const config = require('./config.json')
const greeting = config.greeting;
console.log(greeting)

export function greeter(name: string): string {
    return greeting + ", " + name
}

__ tests __ / greeter.test.ts:

import {greeter} from '../src/modules/greeter';

test('greeter uses greeting specifies in the configuration file', () => {
  expect(greeter('ABC')).toBe('hi, ABC');
});

test('greeter uses the name specified as its runtime argument', () => {
  expect(greeter('ABC')).toContain('ABC');
});

jest.config.js:

module.exports = {
    "verbose": true,
    "transform": {
        "^.+\\.tsx?$": "ts-jest"
    },
    // "moduleDirectories": [
    //     "node_modules", 
    //     "<rootDir>/__tests__/"
    // ],
    // "modulePaths": [
    //     "<rootDir>/__tests__/"
    // ]
};

package.json:

{
  ...
  "main": "index.js",
  "scripts": {
    "clean": "rm -rf ./build/",
    "test": "jest --verbose --coverage --debug --env node",
    "build": "npx rollup -c",
    "start": "node ./build/app.js"
  },
  ...
  "devDependencies": {
    "@types/jest": "^24.0.11",
    "@types/node": "^11.13.0",
    "jest": "^24.7.1",
    "rollup": "^1.9.0",
    "rollup-plugin-copy": "^1.0.0",
    "rollup-plugin-typescript": "^1.0.1",
    "ts-jest": "^24.0.2",
    "tslib": "^1.9.3",
    "typescript": "^3.4.2"
  },
  "dependencies": {}
}

这个想法是,构建会创建app.js捆绑软件,该捆绑软件将从config.json中读取配置,该捆绑软件应位于同一目录中。

config.json:

{
    "greeting": "hi"
} 

捆绑软件app.js包含:

var config = require('./config.json');

...并且运行app.js可以很好地工作(在提供config.json的情况下),但是当我运行npm test(或简单地npx jest)时,开玩笑的错误与:

 FAIL  __tests__/greeter.test.ts
  ● Test suite failed to run

    Cannot find module './config.json' from 'greeter.ts'

    However, Jest was able to find:
        '../src/modules/greeter.ts'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

    > 1 | const config = require('./config.json')
        |                ^
      2 | const greeting = config.greeting;
      3 | console.log(greeting)
      4 | 

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.<anonymous> (src/modules/greeter.ts:1:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.03s
Ran all test suites.

要纠正此错误,我需要将config.json(仅用于单元测试)保留在greeter.ts(在src/modules中)旁边,但我想将其保留在{ {1}}。我试图将Jest配置文件中的 moduleDirectories modulePaths 更改为无效。

是否有一种方法可以使__tests__在Jest测试下对config.json模块可见,同时在运行时保持来自软件包greeter.ts的可见性?

1 个答案:

答案 0 :(得分:0)

简短的回答是“否”。您可以不需要不存在的文件。

更长的答案-我不清楚您为什么要这样做。无论测试文件的结构如何,您的源代码都应完整。您将不会引用不存在的文件,这是当require('./config.json')不存在时从src/modules/greeter.ts src/modules/config.json进行的操作。

因此,我建议将config.json的副本移到您的modules目录中,如果需要beforeAll的内容,请在笑话文件中使用config.json出于测试目的而有所不同。

希望这会有所帮助!