我有一个简单的Svelte组件:
src / App.svelte
<script>
import Datepicker from 'svelte-calendar';
let selected = new Date();
</script>
<Datepicker bind:selected/>
<h1>Hello World!</h1>
它运行得很好,但我也想对其进行测试:
src / App.test.js
import App from "./App.svelte";
import { render } from "@testing-library/svelte";
it("it works", async () => {
const { component } = render(App);
expect(component).toHaveTextContent("Hello");
});
jest.config.js
module.exports = {
transform: {
"^.+\\.svelte$": "svelte-jester",
"^.+\\.js$": "babel-jest",
},
moduleFileExtensions: ["js", "svelte"],
moduleDirectories: ["node_modules", "src"],
modulePaths: ["<rootDir>/src", "<rootDir>/node_modules"],
};
package.json
{
"name": "svelte-app",
"version": "1.0.0",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public",
"test": "jest"
},
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@rollup/plugin-commonjs": "^12.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@testing-library/svelte": "^3.0.0",
"babel-jest": "^26.1.0",
"jest": "^26.1.0",
"rollup": "^2.3.4",
"rollup-plugin-livereload": "^1.0.0",
"rollup-plugin-svelte": "^5.0.3",
"rollup-plugin-terser": "^5.1.2",
"svelte": "^3.0.0",
"svelte-jester": "^1.0.6"
},
"dependencies": {
"sirv-cli": "^1.0.0",
"svelte-calendar": "^1.5.0"
}
}
开玩笑地运行此测试时,它会显示Cannot find module 'svelte-calendar' from 'src/App.svelte'
。我已经尝试将node_modules
添加到jest.config.js
中的moduleDirectories和modulePaths中。我该如何工作?