我正在尝试将我的测试从 jest 迁移到 vitest。我有一个测试套件,它使用 dotenv 包来提取我的 .env 变量。
我的测试套件中有这个
beforeAll(async () => {
vi.clearAllMocks();
cleanUpMetadata();
dotenv.config();
controller = new UserController(container.get<UserServiceLocator>(Symbol.for("UserServiceLocator")),
container.get<EmailServiceLocator>(Symbol.for("EmailServiceLocator")));
});
Run Code Online (Sandbox Code Playgroud)
这是测试中具有未定义变量的代码
let requestObj = httpMocks.createRequest({
cookies: {
token: jwt.sign({ username: "testusername" }, process.env.JWT_SECRET_KEY!)
}
});
Run Code Online (Sandbox Code Playgroud)
为了让我的 .env 变量可访问,我必须做一些特别的事情吗?
我有一个使用 jest 的项目,我可以运行 jest,npm test如果我不设置预设,它就可以工作。
我需要预设@shelf/jest-mongodb,我收到了这篇文章标题中的错误。
这是我的 jest.config.js:
const path = require('path')
module.exports = {
preset: '@shelf/jest-mongodb',
rootDir: path.resolve(__dirname),
testMatch: ['<rootDir>\\test\\unit\\specs\\**.js'],
moduleFileExtensions: [
'js',
'json',
'vue'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
},
testPathIgnorePatterns: [
'<rootDir>/test/e2e',
'<rootDir>/test/unit/specs/bundle'
],
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
setupFiles: ['<rootDir>/test/unit/setup'],
coverageDirectory: '<rootDir>/test/unit/coverage',
collectCoverageFrom: [
'src/**/*.{js,vue}',
'!src/main.js',
'!src/router/index.js',
'!**/node_modules/**'
],
verbose: true,
testURL: 'http://localhost/'
}
Run Code Online (Sandbox Code Playgroud)
我想重申,如果我删除“预设”选项,测试将按预期运行。
我已经在“预设”字段中尝试了我能想到的所有字符组合来让它找到 node_module,但它没有找到它。
如果有帮助,这里是预设的文档:
这仍然没有解决。
我将添加文件夹结构以查看是否有帮助:
????.vscode
????build
????config
????node_modules
????@shelf
? ????jest-mongodb …Run Code Online (Sandbox Code Playgroud)