Skip to content

Test

AVA

Crea una nou projecte test amb compatibilitat esm:

Terminal window
$ mkdir test && cd test
$ npm init -y esm
Wrote to /home/david/test/package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Pots veure que s’ha creat un fitxer package.json.

Si executem npm run test es produirà un error perqué l’script amb index test executa ... && exit 1 tal com pots veure en la definició del fitxer package.json :

Terminal window
$ npm run test
> test@1.0.0 test
> echo "Error: no test specified" && exit 1
Error: no test specified

A continuació instal.lem i configurem ava:

Terminal window
$ npm init ava

Pots veure que s’ha modificat el fitxer package.json:

Terminal window
$ cat package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"module": "main.js",
"scripts": {
"test": "ava"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"esm": "^3.2.25"
},
"devDependencies": {
"ava": "^6.1.3"
}
}

S’ha instal.lat una dependència amb el paquet ava i s’ha modificat l’script "test" perqué executi l’ordre ava.

Quan ara executem npm run test, l’script executa ava i es produeix un error perquè avano ha pogut trobar cap test.

Terminal window
$ npm run test
> test@1.0.0 test
> ava
Couldn’t find any files to test

Crea un nou fitxer en el directori arrel amb el nom test.js:

const test = require("ava");
test('foo', t => {
t.pass();
});
test('bar', async t => {
const bar = Promise.resolve('bar');
t.is(await bar, 'bar');
});

Executa l’script test (que executara ava):

Terminal window
$ npm test
> test@1.0.0 test
> ava
foo
bar
2 tests passed