π° vue2 jest νκ²½μΈν
π Vue2 jest νκ²½μΈν νλ μ΄μ
μ΄λ²μ νμ¬ νλ‘μ νΈμ ν μ€νΈ μ½λ λμ νκΈ° μμ νκ²½μΈν μ μ€λΉ νμμ±μ λκ»΄μ λΈλ‘κ·Έμ λ¨κΈ°κ³ μ νλ€.
νμ νκ²½μΈν μ΄ μ€λ걸리기λ νλ€. νκ²½μΈν μ μ¬κΈ°μ μ°Έκ³ νμλ€.
νκ²½μ μλμ κ°λ€.
- Node Version : 16.19.1
- Npm Version : 8.19.3
βοΈ λ·° ν
μ€νΈ λΌμ΄λΈλ¬λ¦¬ μ€μΉ
npm install jest --save-dev
npm install @vue/test-utils@1.3.6 --save-dev
npm install vue-jest --save-dev
npm install babel-jest --save-dev
βοΈ λ·° ν μ€νΈ λΌμ΄λΈλ¬λ¦¬ μ€μΉ (μμ λ³Έ)
npm install @vue/cli-plugin-unit-jest@5.0.0 --save-dev
npm install @vue/test-utils@1.3.6 --save-dev
npm install @vue/vue2-jest@27.0.0 --save-dev
π― Babel(2023.08.10) μμ
λ°λ²¨μ μ΅μ λ¬Έλ²μ es5 μΌλ‘ λ°κΏμ£Όλ μν μ νλ€. jest κ΄λ ¨νμ¬ babel μ€μ μ΄ νμνλ€.
(λΉμ λ²μ μ νΈνμ±μ΄ λ§μ§ μμ install ν λ λ²μ μ λͺ μν΄μ£Όμλ€.)
npm install babel-core@^7.0.0-bridge.0 --save-dev
npm install babel-jest@27.3.1 --save-dev
npm install @babel/core@7.0.0-bridge.0 --save-dev
npm install @babel/preset-env@7.22.10 --save-dev
vue2μμ babel.config.jsμ μλμ²λΌ λμ΄μλ€λ©΄ babel/presetμ μΈν ν νμκ° μλ€.
module.exports = {
presets: [
//"@babel/preset-env", μλμ€μ μνλ κ²½μ°
'@vue/cli-plugin-babel/preset' //vue cliμμ μλμ€μ λ¨
]
}
π’ vue jest λͺ
λ Ήμ΄ μ¬μ©ν μ μλλ‘ μΆκ°
//vue add unit-jest
βοΈ jest μ€μΉ
npm install jest@27.0.3 --save-dev
π jest.config.js
jestλ₯Ό μ¬μ©νκΈ°μν΄μλ jest νκ²½μ€μ μ΄ νμνκ³ jest.config.jsμμ μμ μ΄ κ°λ₯νλ€.
module.exports = {
preset: '@vue/cli-plugin-unit-jest',
moduleFileExtensions: [
"js",
"json",
// λͺ¨λ vue νμΌ(`*.vue`)μ μ²λ¦¬νκΈ° μν΄ Jestμκ² μλ €μ€λλ€
"vue"
],
transform: {
".*\\.(vue)$": "@vue/vue2-jest",
".*\\.(js)$": "babel-jest",
},
moduleNameMapper: {
// λ³μΉ @(νλ‘μ νΈ/src) μ¬μ©νμ¬ νμ κ²½λ‘μ νμΌμ λ§΅νν©λλ€
'^@/(.*)$': '<rootDir>/src/$1'
},
testMatch: [
// __tests__ κ²½λ‘ νμμ μλ λͺ¨λ js/ts/jsx/tsx νμΌμ ν
μ€νΈ λμμΌλ‘ μ§μ ν©λλ€
"**/__tests__/**/*.[jt]s?(x)",
// νμΌ μ΄λ¦μ 'xxx.spec' λλ 'xxx.test'λΌλ μ΄λ¦μ΄ λΆμ¬μΈ λͺ¨λ js/ts/jsx/tsx νμΌμ ν
μ€νΈ λμμΌλ‘ μ§μ ν©λλ€
"**/?(*.)+(spec|test).[jt]s?(x)"
],
testPathIgnorePatterns: ["/node_modules/"],
collectCoverage: true,
collectCoverageFrom: [
"**/*.{js,vue}",
"!**/node_modules/**",
"!**/coverage/**",
"!**vue.config.js**",
"!**babel.config.js**",
"!**jest.config.js**",
]
}
π©οΈ λͺ λ Ήμ΄ λ±λ‘
//package.json
{
"scripts": {
"test:unit": "vue-cli-service test:unit"
}
}
π₯ HelloWorld.vue μμ±
/*HelloWorld.vue*/
<template>
<div class="hello">
hello
</div>
</template>
<script>
export default {
name: 'HelloWorld',
}
</script>
<style scoped></style>
//helloworldcomponent.test.js
import { shallowMount } from "@vue/test-utils";
import HelloWorld from '@/components/HelloWorld.vue'
describe('hello world', () => {
test('test1', async () => {
const wrapper = shallowMount(HelloWorld);
expect(wrapper.text()).toBe("hello")
});
});
μλ λͺ λ Ήμ΄λ₯Ό ν΅ν΄ HelloWorld.vueκ° ν μ€νΈμ½λλ₯Ό μ±κ³΅μ μΌλ‘ ν΅κ³Όν κ²μ μ μ μλ€.
npm run test:unit