Skip to content

Commit

Permalink
Merge pull request #115 from chanthafef/master
Browse files Browse the repository at this point in the history
support aliases with ``serverless-bundle test``
  • Loading branch information
jayair authored Jul 19, 2020
2 parents a23135e + 4471392 commit fd4945a
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 3,841 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ custom:
import Utility from 'Lib/utility';
```

To use aliases in your tests you'll need to use Jest's [`moduleNameMapper`](https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring). Add the following your `package.json`:

``` json
"jest": {
"moduleNameMapper": {
"Lib(.*)$": "<rootDir>/custom-lib/src/lib/$1"
}
}
```

- Usage with WebStorm

Here is some info on how to get this plugin to support running tests in WebStorm — https://github.com/AnomalyInnovations/serverless-bundle/issues/5#issuecomment-582237396
Expand Down Expand Up @@ -340,7 +350,7 @@ $ npm test
To test the `serverless-bundle test` command.

```bash
$ npm run test-scripts
$ npm run test scripts
```

To install locally in another project.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
],
"testPathIgnorePatterns": [
"<rootDir>/scripts",
"<rootDir>/tests/scripts/tests"
"<rootDir>/tests/scripts/tests",
"<rootDir>/tests/aliases-jest/tests"
]
},
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion scripts/config/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module.exports = (resolve, rootDir) => {
"testResultsProcessor",
"transform",
"transformIgnorePatterns",
"watchPathIgnorePatterns"
"watchPathIgnorePatterns",
"moduleNameMapper"
];
if (overrides) {
supportedKeys.forEach(key => {
Expand Down
5 changes: 5 additions & 0 deletions tests/aliases-jest/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"parserOptions": {
"sourceType": "module"
}
}
16 changes: 16 additions & 0 deletions tests/aliases-jest/aliases-jest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { runJestCommand, clearNpmCache } = require("../helpers");

beforeEach(async () => {
await clearNpmCache(__dirname);
});

afterAll(async () => {
await clearNpmCache(__dirname);
});

test("aliases-jest", async () => {
const result = await runJestCommand(__dirname);

// Ensure Jest ran the included tests successfully
expect(result).not.toMatch(/failed/);
});
11 changes: 11 additions & 0 deletions tests/aliases-jest/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sum from "libs/sum";

const res = (body = {}) => ({
statusCode: 200,
body: JSON.stringify(body, null, 2)
});

export const hello = () =>
res({
sum: sum(1, 2, 3)
});
1 change: 1 addition & 0 deletions tests/aliases-jest/libs/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (...numbers) => numbers.reduce((acc, nbr) => acc + nbr);
5 changes: 5 additions & 0 deletions tests/aliases-jest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/aliases-jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "aliases-jest",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "../../bin/scripts.js test --testPathIgnorePatterns=\"./aliases-jest.test.js\""
},
"jest": {
"moduleNameMapper": {
"libs(.*)$": "<rootDir>/libs/$1"
}
},
"keywords": [],
"author": "",
"license": "ISC"
}
12 changes: 12 additions & 0 deletions tests/aliases-jest/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
service: my-service

plugins:
- '../../index'

provider:
name: aws
runtime: nodejs10.x

functions:
hello:
handler: handler.hello
8 changes: 8 additions & 0 deletions tests/aliases-jest/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { hello } from "../handler";

test("hello returns sum equal 6 in body", () => {
const expected = 6;
const actual = JSON.parse(hello().body).sum;

expect(actual).toBe(expected);
});
Loading

0 comments on commit fd4945a

Please sign in to comment.