Skip to content

Commit

Permalink
Jest added yeoman#759
Browse files Browse the repository at this point in the history
- Updated generator tests
- Improved combined tests behaviour (e2e + unit) as single task
- Added Jest sample test based in main.js' greetings function
  • Loading branch information
UlisesGascon committed Jun 5, 2019
1 parent 73761e0 commit 69949d0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
4 changes: 4 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ module.exports = class extends Generator {
copy('demo_cypress.js', 'cypress/integration/index_spec.js');
}

if (this.includeUnit && this.unitTestFramework === 'jest') {
copy('demo_jest.js', '__test__/main.test.js');
}

let cssFile = `main.${this.includeSass ? 'scss' : 'css'}`;
copyTpl(cssFile, `app/styles/${cssFile}`, templateData);
}
Expand Down
16 changes: 15 additions & 1 deletion app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"gulp-sourcemaps": "^2.2.0",
"gulp-uglify": "^3.0.1",
"gulp-useref": "^3.0.0",
<%_ if (includeUnit && unitTestFramework === 'jest') { -%>
"jest": "^24.8.0",
<%_ } -%>
<%_ if (includeModernizr) { -%>
"mkdirp": "^0.5.1",
<%_ } -%>
Expand All @@ -54,6 +57,11 @@
"yargs": "12.0.5"
},
"scripts": {
<%_ if (includeUnit && unitTestFramework === "jest") { -%>
"test:unit": "jest",
"test:unit-coverage": "jest --coverage",
"test:unit-watch": "jest --watchAll",
<%_ } -%>
<%_ if (includeE2e) { -%>
"test:e2e-open": "cypress open",
"test:e2e": "start-server-and-test start http://localhost:9000 test:e2e-open",
Expand All @@ -62,9 +70,15 @@
"serve:dist": "cross-env NODE_ENV=production gulp serve",
"start": "gulp serve",
"build": "cross-env NODE_ENV=production gulp",
<%_ if (includeE2e) { -%>
<%_ if (includeE2e && !includeUnit) { -%>
"test": "npm run test:e2e",
<%_ } -%>
<%_ if (includeUnit && !includeE2e) { -%>
"test": "npm run test:unit",
<%_ } -%>
<%_ if (includeE2e && includeUnit) { -%>
"test": "npm run test:unit && npm run test:e2e",
<%_ } -%>
"tasks": "gulp --tasks"
},
"browserslist": [
Expand Down
5 changes: 5 additions & 0 deletions app/templates/demo_jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { greeting } from '../app/scripts/main.js'

test('Say Allo!', () => {
expect(greeting()).toBe('\'Allo \'Allo!');
});
70 changes: 55 additions & 15 deletions test/tests-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ const path = require('path');
const helpers = require('yeoman-test');
const assert = require('yeoman-assert');

function jestPresent() {
assert.fileContent('package.json', '"test:unit": "jest"');
assert.fileContent('package.json', '"test:unit-coverage": "jest --coverage"');
assert.fileContent('package.json', '"test:unit-watch": "jest --watchAll"');
assert.fileContent('package.json', '"jest": ');
assert.file('__test__/main.test.js');
}

function jestNotPresent() {
assert.noFileContent('package.json', '"test:unit": "jest"');
assert.noFileContent(
'package.json',
'"test:unit-coverage": "jest --coverage"'
);
assert.noFileContent('package.json', '"test:unit-watch": "jest --watchAll"');
assert.noFileContent('package.json', '"jest": ');
assert.noFile('__test__/main.test.js');
}

function unitPresent() {
assert.fileContent('app/index.html', 'type="module" src="scripts/main.js"');
assert.fileContent('app/scripts/main.js', 'export function greeting');
Expand Down Expand Up @@ -37,13 +56,17 @@ describe('Test handler', () => {
.on('end', done);
});

it.skip('Should not add unit tests', () => {
it('Should not add unit tests', () => {
unitNotPresent();
});

it('Should not add e2e tests', () => {
e2eNotPresent();
});

it('Should not create tests tasks', () => {
assert.noFileContent('package.json', '"test": ');
});
});

describe('When user agrees to add test but avoid to pick unit and e2e test', () => {
Expand All @@ -54,13 +77,17 @@ describe('Test handler', () => {
.on('end', done);
});

it.skip('Should not add any unit tests', () => {
it('Should not add any unit tests', () => {
unitNotPresent();
});

it('Should not add e2e tests', () => {
e2eNotPresent();
});

it('Should not create tests tasks', () => {
assert.noFileContent('package.json', '"test": ');
});
});

describe('When user agrees to test only e2e', () => {
Expand All @@ -71,13 +98,20 @@ describe('Test handler', () => {
.on('end', done);
});

it.skip('Should not add any unit tests', () => {
it('Should not add any unit tests', () => {
unitNotPresent();
});

it('Should add e2e tests', () => {
e2ePresent();
});

it('Should not create combined tests task', () => {
assert.noFileContent(
'package.json',
'"test": "npm run test:unit && npm run test:e2e"'
);
});
});

describe('When user agrees to e2e testing add Unit Testing (Ava)', () => {
Expand All @@ -100,13 +134,19 @@ describe('Test handler', () => {
unitPresent();
});

it('Should create combined tests task', () => {
assert.fileContent(
'package.json',
'"test": "npm run test:unit && npm run test:e2e"'
);
});

it.skip('Should add unit tests for Ava', () => {
unitPresent();
//@TODO
});

it.skip('Should not add unit tests for Jest', () => {
//@TODO
it('Should not add unit tests for Jest', () => {
jestNotPresent();
});

it.skip('Should not add unit tests for Jasmine', () => {
Expand Down Expand Up @@ -138,12 +178,12 @@ describe('Test handler', () => {
e2eNotPresent();
});

it.skip('Should add unit tests Basics', () => {
it('Should add unit tests Basics', () => {
unitPresent();
});

it.skip('Should add unit tests for Jest', () => {
//@TODO
it('Should add unit tests for Jest', () => {
jestPresent();
});

it.skip('Should not add unit tests for Ava', () => {
Expand Down Expand Up @@ -187,8 +227,8 @@ describe('Test handler', () => {
//@TODO
});

it.skip('Should not add unit tests for Jest', () => {
//@TODO
it('Should not add unit tests for Jest', () => {
jestNotPresent();
});

it.skip('Should not add unit tests for Ava', () => {
Expand Down Expand Up @@ -232,8 +272,8 @@ describe('Test handler', () => {
//@TODO
});

it.skip('Should not add unit tests for Jest', () => {
//@TODO
it('Should not add unit tests for Jest', () => {
jestNotPresent();
});

it.skip('Should not add unit tests for Ava', () => {
Expand Down Expand Up @@ -277,8 +317,8 @@ describe('Test handler', () => {
//@TODO
});

it.skip('Should not add unit tests for Jest', () => {
//@TODO
it('Should not add unit tests for Jest', () => {
jestNotPresent();
});

it.skip('Should not add unit tests for Ava', () => {
Expand Down

0 comments on commit 69949d0

Please sign in to comment.