|
| 1 | +## Using with TypeScript |
| 2 | + |
| 3 | +> An example project for this setup is available on [GitHub](https://github.com/vuejs/vue-test-utils-typescript-example). |
| 4 | +
|
| 5 | +TypeScript is a popular superset of JavaScript that adds types and classes on top of regular JS. Vue Test Utils includes types in the distributed package, so it works well with TypeScript. |
| 6 | + |
| 7 | +In this guide, we'll walk through how to setup a testing setup for a TypeScript project using Jest and Vue Test Utils from a basic Vue CLI TypeScript setup. |
| 8 | + |
| 9 | +#### Adding TypeScript |
| 10 | + |
| 11 | +First you need to create a project. If you don't have Vue CLI installed, install it globally: |
| 12 | + |
| 13 | +```shell |
| 14 | +$ npm install -g @vue/cli-service-global |
| 15 | +``` |
| 16 | + |
| 17 | +And create a project by running: |
| 18 | + |
| 19 | +```shell |
| 20 | +$ vue create hello-world |
| 21 | +``` |
| 22 | + |
| 23 | +In the CLI prompt, choose to manually select features, select TypeScript, and press enter. This will create a project with TypeScript already configured. |
| 24 | + |
| 25 | +::: tip NOTE |
| 26 | +If you want a more detailed guide on setting up Vue with TypeScript, checkout the [TypeScript Vue starter guide](https://github.com/Microsoft/TypeScript-Vue-Starter). |
| 27 | +::: |
| 28 | + |
| 29 | +The next step is to add Jest to the project. |
| 30 | + |
| 31 | +#### Setting up Jest |
| 32 | + |
| 33 | +Jest is a test runner developed by Facebook, aiming to deliver a battery-included unit testing solution. You can learn more about Jest on its [official documentation](https://facebook.github.io/jest/). |
| 34 | + |
| 35 | +Install Jest and Vue Test Utils: |
| 36 | + |
| 37 | +```bash |
| 38 | +$ npm install --save-dev jest @vue/test-utils |
| 39 | +``` |
| 40 | + |
| 41 | +Next define a `test:unit` script in `package.json`. |
| 42 | + |
| 43 | +```json |
| 44 | +// package.json |
| 45 | +{ |
| 46 | + // .. |
| 47 | + "scripts": { |
| 48 | + // .. |
| 49 | + "test:unit": "jest" |
| 50 | + } |
| 51 | + // .. |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Processing SFCs in Jest |
| 56 | + |
| 57 | +To teach Jest how to process `*.vue` files, we need to install and configure the `vue-jest` preprocessor: |
| 58 | + |
| 59 | +``` bash |
| 60 | +npm install --save-dev vue-jest |
| 61 | +``` |
| 62 | + |
| 63 | +Next, create a `jest` block in `package.json`: |
| 64 | + |
| 65 | +``` json |
| 66 | +{ |
| 67 | + // ... |
| 68 | + "jest": { |
| 69 | + "moduleFileExtensions": [ |
| 70 | + "js", |
| 71 | + "ts", |
| 72 | + "json", |
| 73 | + // tell Jest to handle `*.vue` files |
| 74 | + "vue" |
| 75 | + ], |
| 76 | + "transform": { |
| 77 | + // process `*.vue` files with `vue-jest` |
| 78 | + ".*\\.(vue)$": "vue-jest", |
| 79 | + }, |
| 80 | + "testURL": "http://localhost/" |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Configuring TypeScript for Jest |
| 86 | + |
| 87 | +In order to use TypeScript files in tests, we need to set up Jest to compile TypeScript. For that we need to install `ts-jest`: |
| 88 | + |
| 89 | +``` bash |
| 90 | +$ npm install --save-dev ts-jest |
| 91 | +``` |
| 92 | + |
| 93 | +Next, we need to tell Jest to process JavaScript test files with `ts-jest` by adding an entry under `jest.transform` in `package.json`: |
| 94 | + |
| 95 | +``` json |
| 96 | +{ |
| 97 | + // ... |
| 98 | + "jest": { |
| 99 | + // ... |
| 100 | + "transform": { |
| 101 | + // ... |
| 102 | + // process `*.ts` files with `ts-jest` |
| 103 | + "^.+\\.tsx?$": "ts-jest" |
| 104 | + }, |
| 105 | + // ... |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Placing Test Files |
| 111 | + |
| 112 | +By default, Jest will recursively pick up all files that have a `.spec.js` or `.test.js` extension in the entire project. |
| 113 | + |
| 114 | +To run test files with a `.ts` extension, we need to change the `testRegex` in the config section in the `package.json` file. |
| 115 | + |
| 116 | +Add the following to the `jest` field in `package.json`: |
| 117 | + |
| 118 | +``` json |
| 119 | +{ |
| 120 | + // ... |
| 121 | + "jest": { |
| 122 | + // ... |
| 123 | + "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$" |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +Jest recommends creating a `__tests__` directory right next to the code being tested, but feel free to structure your tests as you see fit. Just beware that Jest would create a `__snapshots__` directory next to test files that performs snapshot testing. |
| 129 | + |
| 130 | +### Writing a unit test |
| 131 | + |
| 132 | +Now we've got the project set up, it's time to write a unit test. |
| 133 | + |
| 134 | +Create a `src/components/__tests__/HelloWorld.spec.ts` file, and add the following code: |
| 135 | + |
| 136 | +```js |
| 137 | +// src/components/__tests__/HelloWorld.spec.ts |
| 138 | +import { shallowMount } from '@vue/test-utils' |
| 139 | +import HelloWorld from '../HelloWorld.vue' |
| 140 | + |
| 141 | +describe('HelloWorld.vue', () => { |
| 142 | + test('renders props.msg when passed', () => { |
| 143 | + const msg = 'new message' |
| 144 | + const wrapper = shallowMount(HelloWorld, { |
| 145 | + propsData: { msg } |
| 146 | + }) |
| 147 | + expect(wrapper.text()).toMatch(msg) |
| 148 | + }) |
| 149 | +}) |
| 150 | +``` |
| 151 | + |
| 152 | +That's all we need to do to get TypeScript and Vue Test Utils working together! |
| 153 | + |
| 154 | +### Resources |
| 155 | + |
| 156 | +- [Example project for this setup](https://github.com/vuejs/vue-test-utils-typescript-example) |
| 157 | +- [Jest](https://facebook.github.io/jest/) |
0 commit comments