๐ 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
'Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ณ HOC์ฌ์ฉํ์ฌ ๋ก์ง ๊ณตํตํ ์ํค๊ธฐ (0) | 2023.11.05 |
---|---|
๐ vue jest ์ฌ์ด๋๋ก ์์ํ๋ฉด์ ๋ฐฐ์ด๊ฒ๋ค (0) | 2023.08.20 |
props์ผ๋ก ํจ์ ๋ฐ๊ธฐ (0) | 2023.07.23 |
๐ vscode ์์ EditorConfig ์ฐ๊ธฐ (0) | 2023.06.02 |
Vue + jest + typescript ์ธํ (0) | 2023.05.01 |
๋๊ธ