说我已经实现了如下模块:
import { foo } from 'dep1'
export const bar = () => foo()
我该如何模拟dep1
以便进行单元测试bar
?
答案 0 :(得分:1)
一种可能的方法是使用ES模块加载器挂钩。
假设dep1
包含有效代码,我们想对其进行模拟。我将创建一个名为mymodule.mocks.mjs
的文件,在其中模拟foo
:
// Regular mymodule.mjs
import { foo } from 'dep1'
export const bar = () => foo()
// Mocked mymodule.mocks.mjs
// dep1.foo() returns string
export const bar = () => 'whatever'
现在,我们应该能够在测试运行期间请求mymodule.mocks.mjs
时加载mymodule.mjs
。
testModuleLoader.mjs
这是一个实现*.mocks.mjs
约定的自定义模块加载器挂钩:
import { existsSync } from 'fs'
import { dirname, extname, basename, join } from 'path'
import { parse } from 'url'
// The 'specifier' is the name or path that we provide
// when we import a module (i.e. import { x } from '[[specifier]]')
export function resolve (
specifier,
parentModuleURL,
defaultResolver
) {
// For built-ins
if ( !parentModuleURL )
return defaultResolver ( specifier, parentModuleURL )
// If the specifier has no extension we provide the
// Michael Jackson extension as the default one
const moduleExt = extname ( specifier ) || '.mjs'
const moduleDir = dirname ( specifier )
const { pathname: parentModulePath } = parse (
parentModuleURL
)
const fileName = basename ( specifier, moduleExt )
// We build the possible mocks' module file path
const mockFilePath = join (
dirname ( parentModulePath ),
moduleDir,
`${fileName}.mocks${moduleExt}`
)
// If there's a file which ends with '.mocks.mjs'
// we resolve that module instead of the regular one
if ( existsSync ( mockFilePath ) )
return defaultResolver ( mockFilePath, parentModuleURL )
return defaultResolver ( specifier, parentModuleURL )
}
仅是将其提供给node
:
node --experimental-modules --loader ./path/to/testModuleLoader.mjs ./path/to/app.mjs
Learn more关于ES模块加载器挂钩。