Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@feathers/cli: introduce option to choose jest for tests instead of mocha #1057

Merged
merged 17 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = generator => {
const { props } = generator;
const config = {
"env": {
"es6": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2017
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
config.env[props.tester] = true;
return config;
};
3 changes: 2 additions & 1 deletion packages/generator-feathers/generators/app/configs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
configDefault: require('./config.default.json.js'),
configProduction: require('./config.production.json.js'),
package: require('./package.json.js')
package: require('./package.json.js'),
eslintrc: require('./eslintrc.json.js')
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ module.exports = function(generator) {
[packager]: version
},
'scripts': {
test: `${packager} run eslint && ${packager} run mocha`,
test: `${packager} run eslint && NODE_ENV= ${packager} run ${props.tester}`,
eslint: `eslint ${lib}/. test/. --config .eslintrc.json`,
dev: `nodemon ${lib}/`,
start: `node ${lib}/`,
mocha: 'mocha test/ --recursive --exit'
start: `node ${lib}/`
}
};
if ('mocha' === props.tester) {
pkg.scripts['mocha'] = 'mocha test/ --recursive --exit';
} else {
pkg.scripts['jest'] = 'jest';
}

return pkg;
};
48 changes: 27 additions & 21 deletions packages/generator-feathers/generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = class AppGenerator extends Generator {
this.devDependencies = [
'nodemon',
'eslint',
'mocha',
'request',
'request-promise'
];
Expand Down Expand Up @@ -78,36 +77,36 @@ module.exports = class AppGenerator extends Generator {
type: 'list',
message: 'Which package manager are you using (has to be installed globally)?',
default: 'npm@>= 3.0.0',
choices: [{
name: 'npm',
value: 'npm@>= 3.0.0'
}, {
name: 'Yarn',
value: 'yarn@>= 0.18.0'
}]
choices: [
{ name: 'npm', value: 'npm@>= 3.0.0' },
{ name: 'Yarn', value: 'yarn@>= 0.18.0' }
]
}, {
type: 'checkbox',
name: 'providers',
message: 'What type of API are you making?',
choices: [{
name: 'REST',
value: 'rest',
checked: true
}, {
name: 'Realtime via Socket.io',
value: 'socketio',
checked: true
}, {
name: 'Realtime via Primus',
value: 'primus',
}],
choices: [
{ name: 'REST', value: 'rest', checked: true },
{ name: 'Realtime via Socket.io', value: 'socketio', checked: true },
{ name: 'Realtime via Primus', value: 'primus', }
],
validate (input) {
if (input.indexOf('primus') !== -1 && input.indexOf('socketio') !== -1) {
return 'You can only pick SocketIO or Primus, not both.';
}

return true;
}
}, {
type: 'list',
name: 'tester',
message: 'Which testing framework do you prefer?',
default: 'mocha',
choices: [
{ name: 'Mocha + assert', value: 'mocha' },
{ name: 'Jest', value: 'jest' }
],
pageSize: 7 // unnecessary; trying to get codeclimate to leave me alone :(
}];

return this.prompt(prompts).then(props => {
Expand Down Expand Up @@ -145,7 +144,7 @@ module.exports = class AppGenerator extends Generator {
);

this.fs.copyTpl(
this.templatePath('app.test.js'),
this.templatePath(`app.test.${props.tester}.js`),
this.destinationPath(this.testDirectory, 'app.test.js'),
context
);
Expand All @@ -155,6 +154,11 @@ module.exports = class AppGenerator extends Generator {
pkg
);

this.fs.writeJSON(
this.destinationPath('.eslintrc.json'),
makeConfig.eslintrc(this)
)

this.fs.writeJSON(
this.destinationPath('config', 'default.json'),
makeConfig.configDefault(this)
Expand All @@ -181,6 +185,8 @@ module.exports = class AppGenerator extends Generator {
save: true
});

this.devDependencies.push(this.props.tester);

this._packagerInstall(this.devDependencies, {
saveDev: true
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const rp = require('request-promise');
const url = require('url');
const app = require('../<%= src %>/app');

const port = app.get('port') || 3030;
const getUrl = pathname => url.format({
hostname: app.get('host') || 'localhost',
protocol: 'http',
port,
pathname
});

describe('Feathers application tests (with jest)', () => {
beforeAll(done => {
this.server = app.listen(port);
this.server.once('listening', () => done());
});

afterAll(done => {
this.server.close(done);
});

it('starts and shows the index page', () => {
expect.assertions(1);
return rp(getUrl()).then(
body => expect(body.indexOf('<html>')).not.toBe(-1)
);
});

describe('404', () => {
it('shows a 404 HTML page', () => {
expect.assertions(2);
return rp({
url: getUrl('path/to/nowhere'),
headers: {
'Accept': 'text/html'
}
}).catch(res => {
expect(res.statusCode).toBe(404);
expect(res.error.indexOf('<html>')).not.toBe(-1);
});
});

it('shows a 404 JSON error without stack trace', () => {
expect.assertions(4);
return rp({
url: getUrl('path/to/nowhere'),
json: true
}).catch(res => {
expect(res.statusCode).toBe(404);
expect(res.error.code).toBe(404);
expect(res.error.message).toBe('Page not found');
expect(res.error.name).toBe('NotFound');
});
});
});
});

This file was deleted.

3 changes: 2 additions & 1 deletion packages/generator-feathers/generators/hook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ module.exports = class HookGenerator extends Generator {
libDirectory: this.libDirectory
}, this.props);
const mainFile = this.destinationPath(this.libDirectory, 'hooks', `${context.kebabName}.js`);
const tester = this.pkg.devDependencies.jest ? 'jest' : 'mocha';

if (!this.fs.exists(mainFile) && context.type) {
this.props.services.forEach(serviceName =>
Expand All @@ -166,7 +167,7 @@ module.exports = class HookGenerator extends Generator {
);

this.fs.copyTpl(
this.templatePath(this.hasAsync ? 'test-async.js' : 'test.js'),
this.templatePath(this.hasAsync ? `test-async.${tester}.js` : `test.${tester}.js`),
this.destinationPath(this.testDirectory, 'hooks', `${context.kebabName}.test.js`),
context
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const feathers = require('@feathersjs/feathers');
const <%= camelName %> = require('../../<%= libDirectory %>/hooks/<%= kebabName %>');

describe('\'<%= name %>\' hook', () => {
let app;

beforeEach(() => {
app = feathers();

app.use('/dummy', {
async get(id) {
return { id };
}
});

app.service('dummy').hooks({
<% if(type){ %><%= type %>: <%= camelName %>()<% } %>
});
});

it('runs the hook', async () => {
expect.assertions(1);
const result = await app.service('dummy').get('test');
expect(result).toEqual({ id: 'test' });
});
});
26 changes: 26 additions & 0 deletions packages/generator-feathers/generators/hook/templates/test.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const feathers = require('@feathersjs/feathers');
const <%= camelName %> = require('../../<%= libDirectory %>/hooks/<%= kebabName %>');

describe('\'<%= name %>\' hook', () => {
let app;

beforeEach(() => {
app = feathers();

app.use('/dummy', {
get(id) {
return Promise.resolve({ id });
}
});

app.service('dummy').hooks({
<% if(type){ %><%= type %>: <%= camelName %>()<% } %>
});
});

it('runs the hook', () => {
return app.service('dummy').get('test').then(result => {
expect(result).toEqual({ id: 'test' });
});
});
});
Loading