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 18 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/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
33 changes: 33 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const webpackConfig = require('./webpack.config.js');

module.exports = function(config) {
config.set({
basePath: '',
browsers: ['ChromeHeadless'],
files: [
'test/unit/common/**/*.test.ts',
'test/unit/browser/**/*.test.ts',
],
frameworks: ['mocha', '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"]
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@
},
"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-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.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');
12 changes: 9 additions & 3 deletions packages/@sfx/sayt-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
"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",
"url": "https://github.com/groupby/sfx-logic.git",
"directory": "packages/@sfx/sayt-plugin"
},
"author": "",
"license": "MIT"
"license": "MIT",
"mocha": {
"spec": ["../../../test-setup.ts"]
}
}
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;
}
18 changes: 18 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,18 @@
import { expect } from 'chai';
import { stub } from 'sinon';
import * as Sample from '../../../src';

describe('Test Import', () => {
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').callThrough();
Sample.testFunc(testData);
expect(getLabelStub.callCount).to.equal(1);
});
});
18 changes: 18 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,18 @@
import { expect } from 'chai';
import { stub } from 'sinon';
import * as Sample from '../../../src';

describe('Test Import', () => {
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').callThrough();
Sample.testFunc(testData);
expect(getLabelStub.callCount).to.equal(1);
geoffcoutts marked this conversation as resolved.
Show resolved Hide resolved
});
});
18 changes: 18 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,18 @@
import { expect } from 'chai';
import { stub } from 'sinon';
import * as Sample from '../../../src';

describe('Test Import', () => {
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').callThrough();
Sample.testFunc(testData);
expect(getLabelStub.callCount).to.equal(1);
});
});
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
9 changes: 9 additions & 0 deletions test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { use } from 'chai';
import { restore as sinonRestore } from 'sinon';
import * as sinonChai from 'sinon-chai';

use(sinonChai);

afterEach(() => {
sinonRestore();
});
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