Skip to content

Commit

Permalink
test: use jest instead of mocha for the test base (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudbesnier authored Apr 20, 2020
1 parent 2018101 commit beee848
Show file tree
Hide file tree
Showing 35 changed files with 2,995 additions and 1,519 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
extends: [
'airbnb-base',
'plugin:sonarjs/recommended',
'plugin:jest/all',
],
plugins: [
'sonarjs',
Expand All @@ -12,8 +13,18 @@ module.exports = {
},
rules: {
'implicit-arrow-linebreak': 0,
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: [
'scripts/*.js',
'test/**/*.js',
]
},
],
'no-console': 0,
'no-param-reassign': 0,
'jest/expect-expect': 0,
'sonarjs/no-collapsible-if': 0,
'sonarjs/no-duplicate-string': 0,
'sonarjs/no-extra-arguments': 0,
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
node_modules/
.env
.nyc_output/
coverage/
yarn-error.log

# generated by tests
test/services/tokens

# IDE
/*.iml
.idea
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
### Added
- Technical - Add sonar lint.
- Technical - Make the CI lint the commit messages.
- Readme - Add the test coverage badge.

### Changed
- Technical - Untrack IDE `.idea` folder.
- Technical - Reduce the complexity of `Prompter.isRequested` method.
- Technical - Introduce conventional commits.
- Technical - Adapt release manager to conventional commits.
- Tests - Add test for `forest environments:create` command.
- Technical - Use Jest instead of Mocha for the test base.

## RELEASE 1.1.0 - 2020-03-20
### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

[![npm package](https://badge.fury.io/js/forest-cli.svg)](https://badge.fury.io/js/forest-cli)
[![Build Status](https://travis-ci.org/ForestAdmin/toolbelt.svg?branch=master)](https://travis-ci.org/ForestAdmin/toolbelt)
![Coverage](https://img.shields.io/badge/coverage-88%25%0A-critical)

The Lumberjacks' toolbelt is the Forest Admin CLI which makes easy to manage your back office application directly from the terminal.

Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'node',
};
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,16 @@
"@commitlint/cli": "8.3.5",
"@commitlint/config-conventional": "8.3.4",
"@commitlint/travis-cli": "8.3.5",
"@fancy-test/nock": "0.1.1",
"@oclif/dev-cli": "1.19.4",
"@oclif/test": "1.2.2",
"chai": "4.2.0",
"chai-subset": "1.6.0",
"eslint": "6.7.2",
"eslint-config-airbnb-base": "14.0.0",
"eslint-config-oclif": "3.1.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-sonarjs": "0.5.0",
"eslint-plugin-jest": "23.3.0",
"fs-extra": "8.1.0",
"globby": "8.0.1",
"jest": "24.9.0",
"husky": "4.2.4",
"mocha": "5.2.0",
"mock-stdin": "1.0.0",
"moment": "2.24.0",
"nock": "12.0.3",
Expand Down Expand Up @@ -97,7 +93,9 @@
"lint": "./node_modules/eslint/bin/eslint.js bin src test",
"postpack": "rm -f oclif.manifest.json",
"prepack": "oclif-dev manifest && oclif-dev readme",
"test": "nyc mocha --forbid-only \"test/**/*.test.js\" --exit",
"readme:update-coverage": "yarn test:coverage && node ./scripts/update-coverage.js",
"test": "jest",
"test:coverage": "jest --coverage",
"version": "oclif-dev readme && git add README.md"
}
}
3 changes: 3 additions & 0 deletions scripts/update-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { CoverageManager } = require('@forestadmin/devops');

new CoverageManager().updateBadge();
12 changes: 0 additions & 12 deletions test/.eslintrc

This file was deleted.

30 changes: 15 additions & 15 deletions test/commands/environments.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const testDialog = require('./test-cli');
const testCli = require('./test-cli');
const EnvironmentCommand = require('../../src/commands/environments');
const {
loginRequired,
loginPasswordDialog,
enter,
arrowDown,
} = require('../fixtures/dialogs');
const { testEnv } = require('../fixtures/envs');
} = require('../fixtures/std');
const { testEnv } = require('../fixtures/env');
const {
notAGoogleAccountNock,
validAuthNock,
projectListNock,
environmentListNock,
} = require('../fixtures/nocks');
notAGoogleAccount,
loginValid,
getProjectListValid,
getEnvironmentListValid,
} = require('../fixtures/api');

describe('environments', () => {
it('should display environment list', () => testDialog({
it('should display environment list', () => testCli({
env: testEnv,
command: () => EnvironmentCommand.run([]),
nock: [
notAGoogleAccountNock(),
validAuthNock(),
projectListNock(),
environmentListNock(),
api: [
notAGoogleAccount(),
loginValid(),
getProjectListValid(),
getEnvironmentListValid(),
],
dialog: [
std: [
...loginRequired,
...loginPasswordDialog,
...arrowDown,
Expand Down
199 changes: 49 additions & 150 deletions test/commands/environments/copy-layout.test.js
Original file line number Diff line number Diff line change
@@ -1,163 +1,62 @@
const Nock = require('@fancy-test/nock').default;
const { test } = require('@oclif/test');
const _ = require('lodash');
const chai = require('chai');
const chaiSubset = require('chai-subset');

const { expect } = chai;
chai.use(chaiSubset);

const fancy = test.register('nock', Nock);
const EnvironmentSerializer = require('../../../src/serializers/environment');
const JobSerializer = require('../../../src/serializers/job');
const authenticator = require('../../../src/services/authenticator');
const testCli = require('./../test-cli');
const CopyLayoutCommand = require('../../../src/commands/environments/copy-layout');
const { testEnv } = require('../../fixtures/env');
const {
getEnvironmentValid,
getEnvironmentValid2,
postCopyLayout,
getJob,
getJobFailed,
getEnvironmentNotFound,
} = require('../../fixtures/api');

describe('environments:copy-layout', () => {
let getAuthToken;
before(() => {
getAuthToken = authenticator.getAuthToken;
authenticator.getAuthToken = () => 'token';
});
after(() => { authenticator.getAuthToken = getAuthToken; });

describe('on an existing destination environment', () => {
describe('on a completed job', () => {
let parsedBody;

fancy
.stdout()
.stderr()
.env({ FOREST_URL: 'http://localhost:3001' })
.nock('http://localhost:3001', (api) => api
.get('/api/environments/324')
.reply(200, EnvironmentSerializer.serialize({
id: '324',
name: 'Staging',
apiEndpoint: 'https://forestadmin-server-staging.herokuapp.com',
isActive: true,
type: 'development',
lianaName: 'forest-express-sequelize',
lianaVersion: '1.3.2',
secretKey: '2c38a1c6bb28e7bea1c943fac1c1c95db5dc1b7bc73bd649a0b113713ee29125',
})))
.nock('http://localhost:3001', (api) => api
.get('/api/environments/325')
.reply(200, EnvironmentSerializer.serialize({
id: '325',
name: 'Production',
apiEndpoint: 'https://forestadmin-server.herokuapp.com',
isActive: true,
type: 'production',
lianaName: 'forest-express-sequelize',
lianaVersion: '1.3.2',
secretKey: '1b91a1c9bb28e4bea3c941fac1c1c95db5dc1b7bc73bd649b0b113713ee18167',
})))
.nock('http://localhost:3001', (api) => api
.post('/api/deployment-requests', (requestBody) => {
parsedBody = requestBody;
return requestBody;
})
.reply(200, {
meta: {
job_id: 78,
},
}))
.nock('http://localhost:3001', (api) => api
.get('/api/jobs/78')
.reply(200, JobSerializer.serialize({
state: 'complete',
progress: '100',
})))
.command(['environments:copy-layout', '325', '324', '-p', '82', '--force'])
.it('should copy the layout', (ctx) => {
expect(ctx.stdout).to.contain('Environment\'s layout Production successfully copied to Staging.');
});

it('should send the correct body', () => {
expect(parsedBody).to.containSubset({
data: {
id: _.isString,
attributes: {
type: 'environment',
from: '325',
to: '324',
},
type: 'deployment-requests',
},
});
});
it('should copy the layout', () => testCli({
env: testEnv,
token: 'any',
command: () => CopyLayoutCommand.run(['325', '324', '-p', '82', '--force']),
api: [
getEnvironmentValid(),
getEnvironmentValid2(),
postCopyLayout(),
getJob(),
],
std: [
{ out: 'Environment\'s layout Production successfully copied to Staging.' },
],
}));
});

describe('on a failed job', () => {
fancy
.stdout()
.stderr()
.env({ FOREST_URL: 'http://localhost:3001' })
.nock('http://localhost:3001', (api) => api
.get('/api/environments/324')
.reply(200, EnvironmentSerializer.serialize({
id: '324',
name: 'Staging',
apiEndpoint: 'https://forestadmin-server-staging.herokuapp.com',
isActive: true,
type: 'development',
lianaName: 'forest-express-sequelize',
lianaVersion: '1.3.2',
secretKey: '2c38a1c6bb28e7bea1c943fac1c1c95db5dc1b7bc73bd649a0b113713ee29125',
})))
.nock('http://localhost:3001', (api) => api
.get('/api/environments/325')
.reply(200, EnvironmentSerializer.serialize({
id: '325',
name: 'Production',
apiEndpoint: 'https://forestadmin-server.herokuapp.com',
isActive: true,
type: 'production',
lianaName: 'forest-express-sequelize',
lianaVersion: '1.3.2',
secretKey: '1b91a1c9bb28e4bea3c941fac1c1c95db5dc1b7bc73bd649b0b113713ee18167',
})))
.nock('http://localhost:3001', (api) => api
.post('/api/deployment-requests')
.reply(200, {
meta: {
job_id: 78,
},
}))
.nock('http://localhost:3001', (api) => api
.get('/api/jobs/78')
.reply(200, JobSerializer.serialize({
state: 'failed',
progress: '10',
})))
.command(['environments:copy-layout', '325', '324', '-p', '82', '--force'])
.exit(1)
.it('should exit with status 1');
it('should exit with status 1', () => testCli({
env: testEnv,
token: 'any',
command: () => CopyLayoutCommand.run(['325', '324', '-p', '82', '--force']),
api: [
getEnvironmentValid(),
getEnvironmentValid2(),
postCopyLayout(),
getJobFailed(),
],
exitCode: 1,
exitMessage: 'Oops, something went wrong.',
}));
});
});

describe('on an unexisting destination environment', () => {
fancy
.stdout()
.stderr()
.env({ FOREST_URL: 'http://localhost:3001' })
.nock('http://localhost:3001', (api) => api
.get('/api/environments/324')
.reply(404))
.nock('http://localhost:3001', (api) => api
.get('/api/environments/325')
.reply(200, EnvironmentSerializer.serialize({
id: '325',
name: 'Production',
apiEndpoint: 'https://forestadmin-server.herokuapp.com',
isActive: true,
type: 'production',
lianaName: 'forest-express-sequelize',
lianaVersion: '1.3.2',
secretKey: '1b91a1c9bb28e4bea3c941fac1c1c95db5dc1b7bc73bd649b0b113713ee18167',
})))
.command(['environments:copy-layout', '325', '324', '-p', '82', '--force'])
.exit(3)
.it('should exit with status 3');
it('should exit with status 3', () => testCli({
env: testEnv,
token: 'any',
command: () => CopyLayoutCommand.run(['325', '324', '-p', '82', '--force']),
api: [
getEnvironmentNotFound(324),
getEnvironmentValid2(),
],
exitCode: 3,
exitMessage: 'Cannot find the target environment 324 on the project 82.',
}));
});
});
Loading

0 comments on commit beee848

Please sign in to comment.