在基于 jest 的测试代码中无法识别经过导入映射的路径

使用了导入映射 · Cocos Creator,将 project/assets/scripts 映射为 scripts/,那么在业务脚本project/assets/scripts/BusinessExample.ts 就可以使用裸说明符导入依赖模块,如下

import AnotherBusinessExample from 'scripts/AnotherBusinessExample'

export class BusinessExample {
    ...
}

希望基于 jest 对业务逻辑进行测试,在 project/tests/BusinessExample.test.ts 使用相对说明符导入目标模块,如下

import { BusinessExample } from '../assets/scripts/BusinessExample'

test('test BusinessExample', () => { ... })

这里会出现一个问题,裸说明符 'scripts/AnotherBusinessExample' 无法识别。

目前引擎在单元测试这里还没有比较好的适配方式,尤其是对引擎模块的导入。

导入映射只工作在编辑器下,如果要在 jest 下起到一样的效果,可能要用 jest 的 moduleNameMapper。

另外你可以装一个 ts-jest,它会识别你 tsconfig 中的 paths 配置,起到一样的效果。

补充: Paths mapping | ts-jest (kulshekhar.github.io)

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。