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

SFX-95: Testing Setup #4

Merged
merged 22 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
dist/
esnext/
coverage/
.nyc_output/
8 changes: 8 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
jmbtutor marked this conversation as resolved.
Show resolved Hide resolved
"require": [
"ts-node/register",
"source-map-support/register"
],
"recursive": true,
"spec": ["test/setup.ts", "test/unit/common/*.test.ts", "test/unit/node/*.test.ts"]
}
8 changes: 8 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"reporter": [
"lcov",
"text-summary"
]
}
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ To build an individual package in response to changes within the `src` directory
yarn dev
```

### Testing
Tests are run with [Mocha](https://mochajs.org/), [Chai](https://www.chaijs.com/), and [Sinon](https://sinonjs.org/) in Node. Alternatively, browser based testing is also supported and uses the [Karma test runner](https://karma-runner.github.io/latest/index.html). The following browsers are tested:

- Chrome

To run tests for a specific plugin package, navigate to its directory and use the following commands based on the desired testing flow:

- To run the tests for a specific package once:
```sh
yarn test
```
- To run the tests for a specific package and watch the `src` and `test` directories to rerun the tests after any changes:
```sh
yarn tdd
```
- To run the tests in a browser environment once:
```sh
yarn test:browser
geoffcoutts marked this conversation as resolved.
Show resolved Hide resolved
```
- To run the tests in a browser environment while watching the `src` and `test` directories for any changes:
```sh
yarn tdd:browser
```

Test coverage is also provided using [Istanbul](https://github.com/istanbuljs/istanbuljs).

##Documentation
The following command will generate documentation for each module in the `packages` directory. It uses [TypeDoc](https://typedoc.org/) and outputs to the `docs` directory.
```sh
Expand Down
34 changes: 34 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const webpackConfig = require('./webpack.config.js');

module.exports = function(config) {
config.set({
basePath: '',
browsers: ['ChromeHeadless'],
files: [
'test/setup.ts',
'test/unit/common/**/*.test.ts',
'test/unit/browser/**/*.test.ts',
],
frameworks: ['mocha', 'chai', 'sinon', 'karma-typescript'],
reporters: ['mocha', 'karma-typescript'],
karmaTypescriptConfig: {
compilerOptions: {
module: "commonjs"
},
tsconfig: "./tsconfig.json"
},
preprocessors: {
'**/*.ts': ['webpack']
},
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only',
},
autoWatch: true,
singleRun: true,
concurrency: Infinity,
port: 9876,
logLevel: config.LOG_INFO,
colors: true,
})
}
4 changes: 4 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ext": "ts,json",
"watch": "src"
}
4 changes: 4 additions & 0 deletions nodemon.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ext": "ts,json",
"watch": ["src", "test"]
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@
},
"homepage": "https://github.com/groupby/sfx-logic#readme",
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@strictsoftware/typedoc-plugin-monorepo": "^0.2.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.0.8",
"chai": "^4.2.0",
"karma": "^4.1.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-sinon": "^1.0.5",
"karma-typescript": "^4.1.0",
"karma-typescript-preprocessor": "^0.4.0",
"karma-webpack": "^4.0.2",
"mocha": "^6.1.4",
"nodemon": "^1.19.1",
"nyc": "^14.1.1",
"sinon": "^7.3.2",
"sinon-chai": "^3.3.0",
"source-map-support": "^0.5.12",
"ts-loader": "^6.0.3",
"ts-node": "^8.3.0",
"typedoc": "^0.14.2",
"typedoc-plugin-nojekyll": "^1.0.1",
"typescript": "^3.5.2",
Expand Down
1 change: 1 addition & 0 deletions packages/@sfx/sayt-plugin/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../../karma.conf.js');
7 changes: 5 additions & 2 deletions packages/@sfx/sayt-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "src/index.ts",
"scripts": {
"build": "../../../scripts/build.sh",
"dev": "nodemon --watch src --exec npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "nodemon --config ../../../nodemon.json --exec npm run build",
"test": "nyc mocha",
"tdd": "nodemon --config ../../../nodemon.test.json --exec npm test",
"test:browser": "karma start karma.conf.js",
"tdd:browser": "karma start karma.conf.js --no-single-run"
},
"repository": {
"type": "git",
Expand Down
12 changes: 8 additions & 4 deletions packages/@sfx/sayt-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
*/
const testObj = {
label: 'test text',
number: 6,
}

const testFunc = (text: TestInterface) => {
console.log(`I am logging out ${ text.label }`);
export const testFunc = (data: DummyInterface) => {
return exports.getLabel(data).toUpperCase();
geoffcoutts marked this conversation as resolved.
Show resolved Hide resolved
}

testFunc(testObj);
export const getLabel = (data: DummyInterface) => {
return data.label;
}

interface TestInterface {
export interface DummyInterface {
label: string;
number: number;
}
1 change: 1 addition & 0 deletions packages/@sfx/sayt-plugin/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../../../test/setup';
20 changes: 20 additions & 0 deletions packages/@sfx/sayt-plugin/test/unit/browser/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, stub } from '../../utils';
import * as Sample from '../../../src';

describe('Test Import Browser', () => {
let testData: Sample.DummyInterface = {
label: 'test',
number: 6,
};
it('should return the label string capitalized', () => {
expect(Sample.testFunc(testData)).to.equal('TEST');
});
it('should call a function to get the label value', () => {
const getLabelStub = stub(Sample, 'getLabel').returns('other return string');
Sample.testFunc(testData);
expect(getLabelStub).to.be.calledOnce;
});
it('test', () => {
expect(Sample.getLabel(testData)).to.equal('test');
});
});
20 changes: 20 additions & 0 deletions packages/@sfx/sayt-plugin/test/unit/common/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, stub } from '../../utils';
Copy link
Contributor

@DanielMoniz-GBI DanielMoniz-GBI Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a source of great pain for me. I didn't manage to get the same file running in Karma and Mocha. I'm curious if you ran into any issues here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plenty of issues.... Every time we added a new tool for testing it would break.

import * as Sample from '../../../src';

describe('Test Import Common', () => {
let testData: Sample.DummyInterface = {
label: 'test',
number: 6,
};
it('should return the label string capitalized', () => {
expect(Sample.testFunc(testData)).to.equal('TEST');
});
it('should call a function to get the label value', () => {
const getLabelStub = stub(Sample, 'getLabel').returns('other return string');
Sample.testFunc(testData);
expect(getLabelStub).to.be.calledOnce;
});
it('test', () => {
expect(Sample.getLabel(testData)).to.equal('test');
});
});
20 changes: 20 additions & 0 deletions packages/@sfx/sayt-plugin/test/unit/node/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, stub } from '../../utils';
import * as Sample from '../../../src';

describe('Test Import Node', () => {
let testData: Sample.DummyInterface = {
label: 'test',
number: 6,
};
it('should return the label string capitalized', () => {
expect(Sample.testFunc(testData)).to.equal('TEST');
});
it('should call a function to get the label value', () => {
const getLabelStub = stub(Sample, 'getLabel').returns('other return string');
Sample.testFunc(testData);
expect(getLabelStub).to.be.calledOnce;
});
it('test', () => {
expect(Sample.getLabel(testData)).to.equal('test');
});
});
1 change: 1 addition & 0 deletions packages/@sfx/sayt-plugin/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../../../../test/utils';
3 changes: 2 additions & 1 deletion packages/@sfx/sayt-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dom"
],
"types": [
"node"
"node",
"mocha"
],
"module": "commonjs",
"moduleResolution": "node",
Expand Down
8 changes: 8 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { chai, sinon } from './utils';
import * as sinonChai from 'sinon-chai';

chai.use(sinonChai);

afterEach(() => {
sinon.restore();
DanielMoniz-GBI marked this conversation as resolved.
Show resolved Hide resolved
});
10 changes: 10 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as sinonImport from 'sinon';
import * as chaiImport from 'chai';

declare var global: any;

export const sinon = global.sinon || sinonImport;
export const chai = global.chai || chaiImport;
DanielMoniz-GBI marked this conversation as resolved.
Show resolved Hide resolved
export const expect = chai.expect;
export const stub = sinon.stub;
export const spy = sinon.spy;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dom"
],
"types": [
"node"
"node",
"mocha"
],
"module": "commonjs",
"moduleResolution": "node",
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
mode: 'development',

entry: {
plugins: './presets/plugins.ts',
plugins: path.resolve(__dirname, 'presets/plugins.ts'),
},

devtool: 'source-map',
Expand Down
Loading