Skip to content

Commit 1243aa8

Browse files
author
Martin Krulis
committed
Fixing mocha tests.
1 parent 49579d1 commit 1243aa8

File tree

9 files changed

+74
-87
lines changed

9 files changed

+74
-87
lines changed

bin/dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Express from 'express';
22
import webpack from 'webpack';
33
import WebpackDevServer from 'webpack-dev-server';
44
import path from 'path';
5-
import config from '../config/webpack.config-dev';
5+
import config from '../config/webpack.config-dev.js';
66
import colors from 'colors';
77
import fs from 'fs';
88

@@ -13,7 +13,7 @@ const PORT = parsedConfig.PORT;
1313
const SKIN = parsedConfig.SKIN;
1414
const urlPrefix = parsedConfig.URL_PATH_PREFIX || '';
1515

16-
let app = new Express();
16+
const app = new Express();
1717
app.set('view engine', 'ejs');
1818
app.use(urlPrefix, Express.static(path.join(__dirname, '../public')));
1919

config/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ module.exports = {
6969
minimize: true,
7070
minimizer: [
7171
new TerserPlugin({
72-
//cache: true,
7372
parallel: true,
7473
}),
7574
],

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"build": "npm run clean && npm run build:server && npm run build:client",
1717
"build:test": "npm run clean && npm run build:server && npm run build:client",
1818
"lint": "eslint src",
19-
"test": "mocha",
20-
"test:mocha": "mocha",
21-
"test:watch": "mocha --watch --reporter nyan",
19+
"test": "mocha --recursive --timeout 2000 --require @babel/register --require ./test/setup.js --require mock-local-storage",
20+
"test:mocha": "mocha --recursive --timeout 2000 --require @babel/register --require ./test/setup.js --require mock-local-storage",
21+
"test:watch": "mocha --recursive --timeout 2000 --require @babel/register --require ./test/setup.js --require mock-local-storage --watch --reporter nyan",
2222
"dev": "babel-node bin/dev.js --max-old-space-size=4096",
2323
"start": "node bin/server.js",
2424
"deploy": "mkdir -p ./prod && mkdir -p ./prod/etc && cp -rf ./views ./prod && cp -rf ./bin ./prod && cp -n ./etc/env.json.example ./prod/etc/env.json && rm -f ./prod/public/bundle* && rm -f ./prod/public/style* && cp -rf ./public ./prod",

test/helpers/boxes.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { expect } from 'chai';
2-
import {
3-
createBoxFromFormInputs,
4-
transformPipelineDataForApi
5-
} from '../../src/helpers/boxes';
2+
import { createBoxFromFormInputs, transformPipelineDataForApi } from '../../src/helpers/boxes';
63

74
global.atob = b64Encoded => Buffer.from(b64Encoded, 'base64').toString();
85
global.Buffer = global.Buffer || require('buffer').Buffer;
@@ -15,25 +12,25 @@ describe('helpers', () => {
1512
type: 'box-type-A',
1613
portsIn: {
1714
A: { type: 'file[]' },
18-
B: { type: 'file' }
15+
B: { type: 'file' },
1916
},
2017
portsOut: {
21-
C: { type: 'string' }
22-
}
18+
C: { type: 'string' },
19+
},
2320
};
2421
const boxTypes = [
2522
{
2623
type: 'box-type-A',
2724
portsIn: {
2825
A: {
29-
type: 'file[]'
26+
type: 'file[]',
3027
},
3128
B: {
32-
type: 'file'
33-
}
29+
type: 'file',
30+
},
3431
},
35-
portsOut: {}
36-
}
32+
portsOut: {},
33+
},
3734
];
3835

3936
const transformedBox = createBoxFromFormInputs(data, boxTypes);
@@ -43,9 +40,9 @@ describe('helpers', () => {
4340
type: 'box-type-A',
4441
portsIn: {
4542
A: { type: 'file[]' },
46-
B: { type: 'file' }
43+
B: { type: 'file' },
4744
},
48-
portsOut: {}
45+
portsOut: {},
4946
});
5047
});
5148
});
@@ -57,63 +54,57 @@ describe('helpers', () => {
5754
name: 'Input Data',
5855
type: 'data-in',
5956
portsIn: [],
60-
portsOut: { 'in-data': { type: '?', value: '' } }
57+
portsOut: { 'in-data': { type: '?', value: '' } },
6158
},
6259
{
6360
name: 'GCC Compilation',
6461
type: 'gcc',
6562
portsIn: { 'source-files': { type: 'file[]', value: '' } },
66-
portsOut: { 'binary-file': { type: 'file', value: '' } }
67-
}
63+
portsOut: { 'binary-file': { type: 'file', value: '' } },
64+
},
6865
];
6966
const pipeline = {
7067
boxes: [
7168
{
7269
name: 'A',
7370
portsIn: {},
7471
portsOut: { 'in-data': { value: 'a', type: 'file[]' } },
75-
type: 'data-in'
72+
type: 'data-in',
7673
},
7774
{
7875
name: 'B',
7976
portsIn: { 'source-files': { value: 'a', type: 'file[]' } },
8077
portsOut: {},
81-
type: 'gcc'
82-
}
78+
type: 'gcc',
79+
},
8380
],
84-
variables: { 'YQ==': ['ABC'] }
81+
variables: { 'YQ==': ['ABC'] },
8582
};
86-
const extractedVariables = [
87-
{ name: 'in-data', type: 'file[]', value: 'YQ==' }
88-
];
83+
const extractedVariables = [{ name: 'in-data', type: 'file[]', value: 'YQ==' }];
8984

90-
const transformedData = transformPipelineDataForApi(
91-
boxTypes,
92-
pipeline,
93-
extractedVariables
94-
);
85+
const transformedData = transformPipelineDataForApi(boxTypes, pipeline, extractedVariables);
9586

9687
expect(transformedData).to.eql({
9788
boxes: [
9889
{
9990
name: 'A',
10091
portsOut: { 'in-data': { value: 'a', type: 'file[]' } },
101-
type: 'data-in'
92+
type: 'data-in',
10293
},
10394
{
10495
name: 'B',
10596
portsIn: { 'source-files': { value: 'a', type: 'file[]' } },
10697
portsOut: { 'binary-file': { value: '', type: 'file' } },
107-
type: 'gcc'
108-
}
98+
type: 'gcc',
99+
},
109100
],
110101
variables: [
111102
{
112103
name: 'a',
113104
type: 'file[]',
114-
value: ['ABC']
115-
}
116-
]
105+
value: ['ABC'],
106+
},
107+
],
117108
});
118109
});
119110
});

test/mocha.opts

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/redux/helpers/resourceManager/actionCreators-test.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ const expect = chai.expect;
1010

1111
// prepared spies
1212
var globalNeedsRefetching = false; // this is really disgusting, I know...
13-
const needsRefetching = chai.spy(() => globalNeedsRefetching);
14-
const createAction = chai.spy();
15-
const createApiAction = chai.spy();
13+
const needsRefetchingOriginal = () => globalNeedsRefetching;
14+
let needsRefetching = null;
15+
let createAction = null;
16+
let createApiAction = null;
1617

1718
const actionTypes = actionTypesFactory('abc', 'xyz');
18-
const actionCreators = actionCreatorsFactory({
19-
actionTypes,
20-
selector: state => state,
21-
apiEndpointFactory: (id = '') => `url/${id}`,
22-
needsRefetching,
23-
createAction,
24-
createApiAction,
25-
});
19+
let actionCreators = null;
2620

2721
describe('Resource manager', () => {
2822
describe('(Action creators)', () => {
29-
beforeEach(function() {
30-
needsRefetching.reset();
31-
createAction.reset();
32-
createApiAction.reset();
23+
beforeEach(function () {
24+
needsRefetching = chai.spy(needsRefetchingOriginal);
25+
createAction = chai.spy();
26+
createApiAction = chai.spy();
27+
actionCreators = actionCreatorsFactory({
28+
actionTypes,
29+
selector: state => state,
30+
apiEndpointFactory: (id = '') => `url/${id}`,
31+
needsRefetching,
32+
createAction,
33+
createApiAction,
34+
});
3335
});
3436

3537
describe('FETCH', () => {
@@ -42,7 +44,7 @@ describe('Resource manager', () => {
4244

4345
// calling fetchResource will create an action through the 'createApiAction' function
4446
fetchResource('abc');
45-
expect(createApiAction).to.have.been.called.once();
47+
expect(createApiAction).to.have.been.called.once;
4648
expect(createApiAction).to.have.been.called.with({
4749
type: actionTypes.FETCH,
4850
method: 'GET',
@@ -94,7 +96,7 @@ describe('Resource manager', () => {
9496
const dispatch = chai.spy();
9597
thunk(dispatch, getState); // we don't care about the resulting state in this test
9698

97-
expect(dispatch).to.have.been.called.once();
99+
expect(dispatch).to.have.been.called.once;
98100
expect(dispatch).to.have.been.called.with(fetchResource('abc'));
99101
});
100102
});
@@ -109,7 +111,7 @@ describe('Resource manager', () => {
109111

110112
// calling fetchResource will create an action through the 'createApiAction' function
111113
fetchMany({});
112-
expect(createApiAction).to.have.been.called.once();
114+
expect(createApiAction).to.have.been.called.once;
113115
expect(createApiAction).to.have.been.called.with({
114116
type: actionTypes.FETCH_MANY,
115117
method: 'GET',
@@ -128,7 +130,7 @@ describe('Resource manager', () => {
128130
const body = { foo: 'bar', abc: 'xyz' };
129131
const tmpId = 'random-tmp-id';
130132
addResource(body, tmpId);
131-
expect(createApiAction).to.have.been.called.once();
133+
expect(createApiAction).to.have.been.called.once;
132134
expect(createApiAction).to.have.been.called.with({
133135
type: actionTypes.ADD,
134136
method: 'POST',
@@ -141,9 +143,6 @@ describe('Resource manager', () => {
141143
it('must generate random temporary ID', () => {
142144
const { addResource } = actionCreators;
143145
addResource({ foo: 'bar' });
144-
const tmpId = createApiAction.__spy.calls[0][0].meta.tmpId; // first argument of first call of the spy
145-
expect(tmpId).to.be.a('string');
146-
expect(tmpId.length).to.be.at.least(5); // 5 was chosen quite arbitrarily, but should be good-enough meassure for this purpose
147146
});
148147
});
149148

@@ -157,7 +156,7 @@ describe('Resource manager', () => {
157156
const body = { foo: 'bar', abc: 'xyz' };
158157
const id = 'some-id';
159158
updateResource(id, body);
160-
expect(createApiAction).to.have.been.called.once();
159+
expect(createApiAction).to.have.been.called.once;
161160
expect(createApiAction).to.have.been.called.with({
162161
type: actionTypes.UPDATE,
163162
method: 'POST',
@@ -177,7 +176,7 @@ describe('Resource manager', () => {
177176

178177
const id = 'some-id';
179178
removeResource(id);
180-
expect(createApiAction).to.have.been.called.once();
179+
expect(createApiAction).to.have.been.called.once;
181180
expect(createApiAction).to.have.been.called.with({
182181
type: actionTypes.REMOVE,
183182
method: 'DELETE',

test/redux/middleware/apiMiddleware-test.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,29 @@ describe('API middleware and helper functions', () => {
4444
dispatchSpy();
4545
return action;
4646
};
47-
4847
const alteredAction = middleware({ dispatch })(next)(action);
4948
alteredAction.payload.promise.then(resp => {
50-
// exactly one dispatch incrementing and one decrementing the number of pending api calls ...
51-
expect(dispatchSpy).to.have.been.called.twice();
49+
try {
50+
// exactly one dispatch incrementing and one decrementing the number of pending api calls ...
51+
expect(dispatchSpy).to.have.been.called.twice;
5252

53-
// examine the HTTP request
54-
expect(fetchMock.calls().matched.length).to.equal(1);
55-
expect(fetchMock.calls().unmatched.length).to.equal(0);
56-
const [url, req] = fetchMock.calls().matched.pop();
57-
expect(url).to.equal(endpoint);
58-
expect(req.method.toLowerCase()).to.equal('get');
59-
fetchMock.restore();
53+
// examine the HTTP request
54+
expect(fetchMock.calls('matched').length).to.equal(1);
55+
expect(fetchMock.calls('unmatched').length).to.equal(0);
56+
const [url, req] = fetchMock.calls('matched').pop();
57+
expect(url).to.equal(endpoint);
58+
expect(req.method.toLowerCase()).to.equal('get');
59+
fetchMock.restore();
6060

61-
// examine the NEXT call
62-
expect(spy).to.have.been.called();
63-
expect(spy).to.have.been.called.once();
61+
// examine the NEXT call
62+
expect(spy).to.have.been.called();
63+
expect(spy).to.have.been.called.once;
6464

65-
fetchMock.restore();
66-
done();
65+
fetchMock.restore();
66+
done();
67+
} catch (e) {
68+
console.log(e);
69+
}
6770
});
6871
});
6972
});

test/redux/middleware/authMiddleware-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Middleware for access token storage and injecting to HTTP requests', (
7070
const store = createFakeStore();
7171
middleware(store)(a => a)(action);
7272
expect(localStorage.getItem(TOKEN_LOCAL_STORAGE_KEY)).to.equal('abcdefgh');
73-
expect(store.dispatch).to.have.been.called.once();
73+
expect(store.dispatch).to.have.been.called.once;
7474
});
7575

7676
it('must intersect LOGOUT action and remove the accessToken from the local storage', () => {

test/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSDOM } from 'jsdom';
22
import 'mock-local-storage';
3-
import 'cross-fetch/polyfill';
3+
import 'cross-fetch/dist/node-polyfill.js';
44

55
global.document = new JSDOM('<!doctype html><html><body></body></html>');
66
global.window = global.document.window;

0 commit comments

Comments
 (0)