Skip to content

Commit 89975ad

Browse files
authored
Merge pull request #6 from kandros/add-tests
Add tests with Jest
2 parents 4c58a7a + 4d84e53 commit 89975ad

File tree

5 files changed

+2201
-31
lines changed

5 files changed

+2201
-31
lines changed

package.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
],
1010
"scripts": {
1111
"prepublish": "npm run build",
12+
"precommit": "npm run test",
1213
"build": "rm -rf dist && tsc",
13-
"test": "npm run lint",
14+
"pretest": "npm run lint",
15+
"test": "jest",
1416
"lint": "tslint -c tslint.json 'src/**/*.ts'"
1517
},
1618
"repository": {
@@ -25,13 +27,28 @@
2527
],
2628
"devDependencies": {
2729
"@types/fs-extra": "2.0.0",
30+
"@types/jest": "^19.2.4",
2831
"@types/lodash": "^4.14.62",
32+
"jest": "^20.0.4",
33+
"mock-fs": "^4.3.0",
34+
"ts-jest": "^20.0.6",
2935
"tslint": "^5.1.0"
3036
},
3137
"dependencies": {
3238
"fs-p": "2.0.0",
3339
"globby": "^6.1.0",
3440
"lodash": "^4.17.4",
3541
"typescript": "^2.2.2"
42+
},
43+
"jest": {
44+
"transform": {
45+
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
46+
},
47+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
48+
"moduleFileExtensions": [
49+
"ts",
50+
"tsx",
51+
"js"
52+
]
3653
}
3754
}

src/typescript.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import * as _ from 'lodash'
44
import { ServerlessFunction } from './types'
55
import * as path from 'path'
66

7+
export function makeDefaultTypescriptConfig() {
8+
const defaultTypescriptConfig: ts.CompilerOptions = {
9+
preserveConstEnums: true,
10+
strictNullChecks: true,
11+
sourceMap: true,
12+
target: ts.ScriptTarget.ES5,
13+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
14+
lib: ['lib.es2015.d.ts'],
15+
rootDir: './',
16+
}
17+
18+
return defaultTypescriptConfig
19+
}
20+
721
export function extractFileNames(functions: { [key: string]: ServerlessFunction }): string[] {
822
return _.values(functions)
923
.map(fn => fn.handler)
@@ -59,15 +73,5 @@ export function getTypescriptConfig(cwd: string): ts.CompilerOptions {
5973
return configParseResult.options
6074
}
6175

62-
const defaultTypescriptConfig: ts.CompilerOptions = {
63-
preserveConstEnums: true,
64-
strictNullChecks: true,
65-
sourceMap: true,
66-
target: ts.ScriptTarget.ES5,
67-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
68-
lib: ['lib.es2015.d.ts'],
69-
rootDir: './',
70-
}
71-
72-
return defaultTypescriptConfig
76+
return makeDefaultTypescriptConfig()
7377
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {extractFileNames} from '../src/typescript'
2+
import {ServerlessFunction} from '../src/types'
3+
4+
const functions: { [key: string]: ServerlessFunction } = {
5+
hello: {
6+
handler: 'my-folder/hello.handler',
7+
package: {
8+
include: [],
9+
exclude: []
10+
}
11+
},
12+
world: {
13+
handler: 'my-folder/my-subfolder/world.handler',
14+
package: {
15+
include: [],
16+
exclude: []
17+
}
18+
},
19+
create: {
20+
handler: 'create.create',
21+
package: {
22+
include: [],
23+
exclude: []
24+
}
25+
},
26+
}
27+
28+
describe('extractFileName', () => {
29+
it('get function filenames from serverless service', () => {
30+
expect(
31+
extractFileNames(functions),
32+
).toEqual(
33+
[
34+
'my-folder/hello.ts',
35+
'my-folder/my-subfolder/world.ts',
36+
'create.ts',
37+
],
38+
)
39+
})
40+
})
41+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {getTypescriptConfig, makeDefaultTypescriptConfig} from '../src/typescript'
2+
3+
describe('getTypescriptConfig', () => {
4+
it(`returns default typescript configuration if the one provided doesn't exist`, () => {
5+
expect(
6+
getTypescriptConfig('/ciaone/my-folder'),
7+
).toEqual(
8+
makeDefaultTypescriptConfig()
9+
)
10+
})
11+
})

0 commit comments

Comments
 (0)