|
| 1 | +--- |
| 2 | +title: Outils de développement |
| 3 | +description: Nuxt.js rend votre développement web agréable. |
| 4 | +--- |
| 5 | + |
| 6 | +> Le test de votre application fait partie du développement web. Nuxt.js vous aide à le rendre aussi facile que possible. |
| 7 | +
|
| 8 | +## Tests End-to-End |
| 9 | + |
| 10 | +[Ava](https://github.com/avajs/ava) est un framework JavaScript de test puissant, mixé avec [jsdom](https://github.com/tmpvar/jsdom), nous pouvons les utiliser pour écrire des tests end-to-end testing facilement. |
| 11 | + |
| 12 | +Tout d'abord, nous devons ajouter ava et jsdom en tant que dépendances de développement: |
| 13 | +```bash |
| 14 | +npm install --save-dev ava jsdom |
| 15 | +``` |
| 16 | + |
| 17 | +Et ajouter un script de test à notre `package.json` et configurer ava pour compiler les fichiers que nous importons dans nos tests. |
| 18 | + |
| 19 | +```javascript |
| 20 | +"scripts": { |
| 21 | + "test": "ava", |
| 22 | +}, |
| 23 | +"ava": { |
| 24 | + "require": [ |
| 25 | + "babel-register" |
| 26 | + ] |
| 27 | +}, |
| 28 | +"babel": { |
| 29 | + "presets": [ |
| 30 | + "es2015" |
| 31 | + ] |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Nous allons écrire nos tests dans le répertoire `test`: |
| 36 | +```bash |
| 37 | +mkdir test |
| 38 | +``` |
| 39 | + |
| 40 | +Mettons que nous avons une page dans `pages/index.vue`: |
| 41 | +```html |
| 42 | +<template> |
| 43 | + <h1 class="red">Hello {{ name }}!</h1> |
| 44 | +</template> |
| 45 | + |
| 46 | +<script> |
| 47 | +export default { |
| 48 | + data () { |
| 49 | + return { name: 'world' } |
| 50 | + } |
| 51 | +} |
| 52 | +</script> |
| 53 | + |
| 54 | +<style> |
| 55 | +.red { |
| 56 | + color: red; |
| 57 | +} |
| 58 | +</style> |
| 59 | +``` |
| 60 | + |
| 61 | +Lorsque nous lançons notre application avec `npm run dev` et que nous visitons [http://localhost:3000](http://localhost:3000), nous voyons notre titre `Hello world!` rouge. |
| 62 | + |
| 63 | +Nous ajoutons notre fichier de test `test/index.test.js`: |
| 64 | + |
| 65 | +```js |
| 66 | +import test from 'ava' |
| 67 | +import Nuxt from 'nuxt' |
| 68 | +import { resolve } from 'path' |
| 69 | + |
| 70 | +// We keep the nuxt and server instance |
| 71 | +// So we can close them at the end of the test |
| 72 | +let nuxt = null |
| 73 | +let server = null |
| 74 | + |
| 75 | +// Init Nuxt.js and create a server listening on localhost:4000 |
| 76 | +test.before('Init Nuxt.js', async t => { |
| 77 | + const rootDir = resolve(__dirname, '..') |
| 78 | + let config = {} |
| 79 | + try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {} |
| 80 | + config.rootDir = rootDir // project folder |
| 81 | + config.dev = false // production build |
| 82 | + nuxt = new Nuxt(config) |
| 83 | + await nuxt.build() |
| 84 | + server = new nuxt.Server(nuxt) |
| 85 | + server.listen(4000, 'localhost') |
| 86 | +}) |
| 87 | + |
| 88 | +// Example of testing only generated html |
| 89 | +test('Route / exits and render HTML', async t => { |
| 90 | + let context = {} |
| 91 | + const { html } = await nuxt.renderRoute('/', context) |
| 92 | + t.true(html.includes('<h1 class="red">Hello world!</h1>')) |
| 93 | +}) |
| 94 | + |
| 95 | +// Example of testing via dom checking |
| 96 | +test('Route / exits and render HTML with CSS applied', async t => { |
| 97 | + const window = await nuxt.renderAndGetWindow('http://localhost:4000/') |
| 98 | + const element = window.document.querySelector('.red') |
| 99 | + t.not(element, null) |
| 100 | + t.is(element.textContent, 'Hello world!') |
| 101 | + t.is(element.className, 'red') |
| 102 | + t.is(window.getComputedStyle(element).color, 'red') |
| 103 | +}) |
| 104 | + |
| 105 | +// Close server and ask nuxt to stop listening to file changes |
| 106 | +test.after('Closing server and nuxt.js', t => { |
| 107 | + server.close() |
| 108 | + nuxt.close() |
| 109 | +}) |
| 110 | +``` |
| 111 | + |
| 112 | +Nous pouvons désormais lancer nos tests: |
| 113 | +```bash |
| 114 | +npm test |
| 115 | +``` |
| 116 | + |
| 117 | +jsdom a certaines limitations parce qu'il n'utilise pas de navigateur. Cependant, cela couvrira la plupart de nos tests. Si vous souhaitez utiliser un navigateur pour tester votre application, vous pouvez consulter [Nightwatch.js] (http://nightwatchjs.org). |
| 118 | + |
| 119 | +## ESLint |
| 120 | + |
| 121 | +> ESLint est un excellent outil pour garder votre code propre |
| 122 | +
|
| 123 | +Vous pouvez ajouter [ESLint](http://eslint.org) assez facilement avec nuxt.js. Ajouter les dépendances npm: |
| 124 | + |
| 125 | +```bash |
| 126 | +npm install --save-dev babel-eslint eslint eslint-config-standard eslint-plugin-html eslint-plugin-promise eslint-plugin-standard |
| 127 | +``` |
| 128 | + |
| 129 | +Puis, configurez ESLint via un fichier `.eslintrc.js` à la racine de votre projet: |
| 130 | +```js |
| 131 | +module.exports = { |
| 132 | + root: true, |
| 133 | + parser: 'babel-eslint', |
| 134 | + env: { |
| 135 | + browser: true, |
| 136 | + node: true |
| 137 | + }, |
| 138 | + extends: 'standard', |
| 139 | + // required to lint *.vue files |
| 140 | + plugins: [ |
| 141 | + 'html' |
| 142 | + ], |
| 143 | + // add your custom rules here |
| 144 | + rules: {}, |
| 145 | + globals: {} |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Ensuite, vous pouvez ajouter un script `lint` à `package.json`: |
| 150 | + |
| 151 | +```js |
| 152 | +"scripts": { |
| 153 | + "lint": "eslint --ext .js,.vue --ignore-path .gitignore ." |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +Vous pouvez alors lancer: |
| 158 | +```bash |
| 159 | +npm run lint |
| 160 | +``` |
| 161 | + |
| 162 | +ESLint va linter tous vos fichiers JavaScript et Vue sauf ceux ignorés par `.gitignore`. |
| 163 | + |
| 164 | +<p class="Alert Alert--info">Une bonne pratique est également d'ajouter `"precommit": "npm run lint"` dans package.json afin de linter votre code automatiquement avant de le commiter.</p> |
0 commit comments