Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkio committed Aug 16, 2022
1 parent 246f106 commit 21a4b28
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": [
"ts"
],
"spec": "./**/*.spec.ts",
"require": "ts-node/register"
}
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,20 @@ An npm package for demonstration purposes using TypeScript to build for both the
1. Create an account with [npm](https://www.npmjs.com/signup) if you don't have one already. Also be sure to enable [two-factor authentication](https://docs.npmjs.com/configuring-two-factor-authentication)
1. Sign in to your npm account in your terminal with `npm login`
1. Run `npm publish --access=public` to publish your package

### Testing

1. `npm i -D mocha @type/mocha chai @types/chai ts-node`
1. Create a new file `.mocharc.json` in the root directory with the following contents:
```json
{
"extension": ["ts"],
"spec": "./**/*.spec.ts",
"require": "ts-node/register"
}
```
1. Create a `tests` folder
1. Create an `index.spec.ts` file in the `tests` folder
1. Write unit tests in the `index.spec.ts` file to test the code in `index.ts`
1. Add a `"test"` property in the `package.json` file and give it a value of `"mocha"`
1. Run `npm test` in your terminal from the root folder of the project
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"clean": "rm -rf ./lib",
"build": "npm run clean && npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p ./configs/tsconfig.esm.json",
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json"
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
"test": "mocha"
},
"repository": {
"type": "git",
Expand All @@ -49,6 +50,13 @@
},
"homepage": "https://github.com/snyk-labs/modern-npm-package#readme",
"devDependencies": {
"@types/mocha": "^9.1.1",
"mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"dependencies": {
"@types/chai": "^4.3.3",
"chai": "^4.3.6"
}
}
27 changes: 27 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'mocha';
import { assert } from 'chai';

import { helloWorld } from '../index';
import npmPackage from '../index';

describe('NPM Package', () => {
it('should be an object', () => {
assert.isObject(npmPackage);
});

it('should have a helloWorld property', () => {
assert.property(npmPackage, 'helloWorld');
});
});

describe('Hello World Function', () => {
it('should be a function', () => {
assert.isFunction(helloWorld);
});

it('should return the hello world message', () => {
const expected = 'Hello World from my example modern npm package!';
const actual = helloWorld();
assert.equal(actual, expected);
});
});

0 comments on commit 21a4b28

Please sign in to comment.