繁体   English   中英

如何导入配置文件,以便在Jest测试下通过构建的bundle和模块找到它?

[英]How to import a config file so it can be found both by built bundle and module under Jest test?

我有一个用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 /模块/ 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": {}
}

我的想法是build创建了app.js包,它从config.json读取配置,该配置应该在同一目录中。

config.json:

{
    "greeting": "hi"
} 

捆绑app.js包含:

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

...并且运行app.js工作正常(当提供config.json时)但是当我运行npm test (或简单地npx jestnpx jest )时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.

为了纠正这个错误,我需要保持在greeter.ts (在src/modules )旁边的config.json (仅用于单元测试),但我想将它保存在__tests__ 我试图在Jest配置文件中更改moduleDirectoriesmodulePaths无济于事。

有没有办法在Jest测试greeter.ts模块可以看到config.json ,同时在运行时保持其对app.js包的可见性?

最简洁的答案是不。 您不能要求不存在的文件。

更长的答案 - 我不清楚你为什么要这样做。 无论测试文件结构如何,您的源代码都应该是完整的。 你不会引用一个不存在的文件,当你require('./config.json')来自src/modules/greeter.tssrc/modules/config.json没有存在。

因此,如果您需要将config.json的内容与测试目的不同,我建议将config.json的副本移动到您的modules目录中,然后在您的jest文件中使用beforeAll

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM