Skip to content

Commit 466662a

Browse files
55: Use typescript for tests (#57)
1 parent 5fe914d commit 466662a

File tree

8 files changed

+131
-55
lines changed

8 files changed

+131
-55
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"plugins": ["@typescript-eslint", "prettier"],
55
"extends": ["standard-with-typescript", "prettier", "prettier/@typescript-eslint", "eslint-config-prettier"],
66
"parserOptions": {
7-
"project": "./tsconfig.json",
7+
"project": "./tsconfig.eslint.json",
88
"ecmaFeatures": {
99
"modules": true
1010
},

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@types/babel__core": "^7.1.8",
4040
"@types/is-dotdir": "^1.0.0",
4141
"@types/is-dotfile": "^2.0.0",
42+
"@types/jest": "^26.0.19",
4243
"@types/node": "^14.0.11",
4344
"@typescript-eslint/eslint-plugin": "3",
4445
"@typescript-eslint/parser": "^3.3.0",
@@ -52,6 +53,7 @@
5253
"eslint-plugin-standard": "4",
5354
"jest": "^26.6.3",
5455
"prettier": "^2.0.5",
56+
"ts-jest": "^26.4.4",
5557
"typescript": "^4.1.3"
5658
},
5759
"bin": {
@@ -63,5 +65,11 @@
6365
"engines": {
6466
"npm": "please-use-yarn",
6567
"node": ">=10"
68+
},
69+
"jest": {
70+
"preset": "ts-jest",
71+
"roots": ["<rootDir>/src"],
72+
"testMatch": ["**/*.(spec|test).ts"],
73+
"testEnvironment": "node"
6674
}
6775
}
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const fs = require('fs')
2-
const {
3-
analyzeProject,
4-
calculateComplexity,
5-
generateBadge,
6-
} = require('../build/codehawk')
7-
const { formatResultsAsTable } = require('../build/cli-util')
8-
const { JsxEmit } = require('typescript')
1+
import * as fs from 'fs'
2+
import { analyzeProject, calculateComplexity, generateBadge } from './codehawk'
3+
import { formatResultsAsTable } from './cli-util'
94

105
const cwd = process.cwd()
11-
const outputMatchesResult = (projectPath) => {
6+
const outputMatchesResult = (projectPath: string): void => {
127
const output = analyzeProject(`${cwd}/${projectPath}`)
138
expect(output).toBeTruthy()
149

15-
const expectedRaw = fs.readFileSync(`${cwd}/${projectPath}/expected.json`)
10+
const expectedRaw = fs.readFileSync(
11+
`${cwd}/${projectPath}/expected.json`,
12+
'utf-8'
13+
)
1614
const expected = JSON.parse(expectedRaw)
1715

1816
expect(output.fullResultsTree).toEqual(expected.fullResultsTree)
@@ -35,7 +33,7 @@ describe('codehawk-cli', () => {
3533
describe('calculateComplexity', () => {
3634
it('generates metrics from a static typescript sample', () => {
3735
const metrics = calculateComplexity(STATIC_SAMPLE, '.ts', true, false)
38-
const expectedMetrics = {
36+
const expectedMetrics: any = {
3937
aggregate: {
4038
cyclomatic: 2,
4139
cyclomaticDensity: 50,
@@ -55,7 +53,6 @@ describe('codehawk-cli', () => {
5553
distinct: 6,
5654
total: 6,
5755
},
58-
time: 10.494,
5956
},
6057
paramCount: 1,
6158
sloc: {
@@ -105,8 +102,7 @@ describe('codehawk-cli', () => {
105102
const logSpy = jest.spyOn(console, 'log')
106103

107104
// Note: whitespace is important for this test to pass
108-
const expectedOutputText =
109-
`
105+
const expectedOutputText = `
110106
Codehawk Static Analysis Results
111107
Top 25 files
112108
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
const { getConfiguration } = require('../build/options')
1+
import * as fs from 'fs'
2+
import { mocked } from 'ts-jest'
23

3-
jest.mock('fs');
4+
import { getConfiguration } from './options'
5+
6+
jest.mock('fs')
7+
8+
const mockedFs = mocked(fs, true)
49

510
describe('when attempting to get the configuration for codehawk', () => {
611
afterEach(() => {
@@ -14,11 +19,12 @@ describe('when attempting to get the configuration for codehawk', () => {
1419
good: 'yes',
1520
}
1621

17-
require('fs').__codehawkConfigFound(true, mockCodehawkJson)
22+
mockedFs.existsSync.mockReturnValue(true)
23+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockCodehawkJson))
1824

1925
const config = getConfiguration('/home/')
2026

21-
expect(config).toEqual(mockCodehawkJson);
27+
expect(config).toEqual(mockCodehawkJson)
2228
})
2329
})
2430

@@ -29,19 +35,22 @@ describe('when attempting to get the configuration for codehawk', () => {
2935
happiness: 5,
3036
}
3137

32-
require('fs').__codehawkConfigFound(false, {
33-
codehawk: mockCodehawkJson,
34-
})
38+
mockedFs.existsSync.mockReturnValueOnce(false)
39+
mockedFs.readFileSync.mockReturnValue(
40+
JSON.stringify({
41+
codehawk: mockCodehawkJson,
42+
})
43+
)
3544

3645
const config = getConfiguration('/home/')
3746

3847
expect(config).toEqual(mockCodehawkJson)
3948
})
4049

41-
it('should throw an error when package.json contains no codehawk config' , () => {
42-
require('fs').__codehawkConfigFound(false, {})
43-
50+
it('should throw an error when package.json contains no codehawk config', () => {
51+
mockedFs.existsSync.mockReturnValue(false)
52+
mockedFs.readFileSync.mockReturnValue(JSON.stringify({}))
4453
expect(() => getConfiguration('/home/')).toThrow()
4554
})
4655
})
47-
});
56+
})

test/__mocks__/fs.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

tsconfig.eslint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": []
4+
}

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
"esModuleInterop": true,
88
"declaration": true
99
},
10-
"include": ["src/**/*"]
10+
"include": ["src/**/*"],
11+
"exclude": [
12+
"**/*.spec.ts",
13+
"**/*.test.ts"
14+
]
1115
}

yarn.lock

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,14 @@
610610
dependencies:
611611
"@types/istanbul-lib-report" "*"
612612

613+
"@types/[email protected]", "@types/jest@^26.0.19":
614+
version "26.0.19"
615+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.19.tgz#e6fa1e3def5842ec85045bd5210e9bb8289de790"
616+
integrity sha512-jqHoirTG61fee6v6rwbnEuKhpSKih0tuhqeFbCmMmErhtu3BYlOZaXWjffgOstMM4S/3iQD31lI5bGLTrs97yQ==
617+
dependencies:
618+
jest-diff "^26.0.0"
619+
pretty-format "^26.0.0"
620+
613621
"@types/json-schema@^7.0.3":
614622
version "7.0.5"
615623
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
@@ -1018,14 +1026,21 @@ browser-process-hrtime@^1.0.0:
10181026
resolved "https://registry.yarnpkg.com/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz#3c9b4b7d782c8121e56f10106d84c0d0ffc94626"
10191027
integrity sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow==
10201028

1029+
1030+
version "0.2.6"
1031+
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
1032+
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
1033+
dependencies:
1034+
fast-json-stable-stringify "2.x"
1035+
10211036
10221037
version "2.1.1"
10231038
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.1.tgz#e6787da20ece9d07998533cfd9de6f5c38f4bc05"
10241039
integrity sha512-gQxTNE/GAfIIrmHLUE3oJyp5FO6HRBfhjnw4/wMmA63ZGDJnWBmgY/lyQBpnDUkGmAhbSe39tx2d/iTOAfglwQ==
10251040
dependencies:
10261041
node-int64 "^0.4.0"
10271042

1028-
buffer-from@^1.0.0:
1043+
buffer-from@1.x, buffer-from@^1.0.0:
10291044
version "1.1.1"
10301045
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
10311046
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
@@ -1842,7 +1857,7 @@ fast-diff@^1.1.2:
18421857
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
18431858
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==
18441859

1845-
fast-json-stable-stringify@^2.0.0:
1860+
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0:
18461861
version "2.1.0"
18471862
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
18481863
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
@@ -2561,7 +2576,7 @@ jest-config@^26.6.3:
25612576
micromatch "^4.0.2"
25622577
pretty-format "^26.6.2"
25632578

2564-
jest-diff@^26.6.2:
2579+
jest-diff@^26.0.0, jest-diff@^26.6.2:
25652580
version "26.6.2"
25662581
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-26.6.2.tgz#1aa7468b52c3a68d7d5c5fdcdfcd5e49bd164394"
25672582
integrity sha512-6m+9Z3Gv9wN0WFVasqjCL/06+EFCMTqDEUl/b87HYK2rAPTyfz4ZIuSlPhY51PIQRWx5TaxeF1qmXKe9gfN3sA==
@@ -2827,7 +2842,7 @@ jest-snapshot@^26.6.2:
28272842
pretty-format "^26.6.2"
28282843
semver "^7.3.2"
28292844

2830-
jest-util@^26.6.2:
2845+
jest-util@^26.1.0, jest-util@^26.6.2:
28312846
version "26.6.2"
28322847
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-26.6.2.tgz#907535dbe4d5a6cb4c47ac9b926f6af29576cbc1"
28332848
integrity sha512-MDW0fKfsn0OI7MS7Euz6h8HNDXVQ0gaM9uW6RjfDmd1DAFcaxX9OqIakHIqhbnmF08Cf2DLDG+ulq8YQQ0Lp0Q==
@@ -2962,20 +2977,20 @@ json-stringify-safe@~5.0.1:
29622977
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
29632978
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
29642979

2980+
[email protected], json5@^2.1.2:
2981+
version "2.1.3"
2982+
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
2983+
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
2984+
dependencies:
2985+
minimist "^1.2.5"
2986+
29652987
json5@^1.0.1:
29662988
version "1.0.1"
29672989
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
29682990
integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
29692991
dependencies:
29702992
minimist "^1.2.0"
29712993

2972-
json5@^2.1.2:
2973-
version "2.1.3"
2974-
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43"
2975-
integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==
2976-
dependencies:
2977-
minimist "^1.2.5"
2978-
29792994
jsprim@^1.2.2:
29802995
version "1.4.1"
29812996
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -3066,6 +3081,11 @@ locate-path@^5.0.0:
30663081
dependencies:
30673082
p-locate "^4.1.0"
30683083

3084+
3085+
version "4.1.2"
3086+
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
3087+
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
3088+
30693089
lodash.sortby@^4.7.0:
30703090
version "4.7.0"
30713091
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -3076,13 +3096,25 @@ lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15:
30763096
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
30773097
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==
30783098

3099+
lru-cache@^6.0.0:
3100+
version "6.0.0"
3101+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
3102+
integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
3103+
dependencies:
3104+
yallist "^4.0.0"
3105+
30793106
make-dir@^3.0.0:
30803107
version "3.1.0"
30813108
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
30823109
integrity sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==
30833110
dependencies:
30843111
semver "^6.0.0"
30853112

3113+
3114+
version "1.3.6"
3115+
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
3116+
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
3117+
30863118
30873119
version "1.0.11"
30883120
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
@@ -3171,6 +3203,11 @@ mixin-deep@^1.2.0:
31713203
for-in "^1.0.2"
31723204
is-extendable "^1.0.1"
31733205

3206+
3207+
version "1.0.4"
3208+
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
3209+
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
3210+
31743211
mkdirp@^0.5.1:
31753212
version "0.5.5"
31763213
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def"
@@ -3573,7 +3610,7 @@ prettier@^2.0.5:
35733610
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.5.tgz#d6d56282455243f2f92cc1716692c08aa31522d4"
35743611
integrity sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==
35753612

3576-
pretty-format@^26.6.2:
3613+
pretty-format@^26.0.0, pretty-format@^26.6.2:
35773614
version "26.6.2"
35783615
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93"
35793616
integrity sha512-7AeGuCYNGmycyQbCqd/3PWH4eOoX/OiCa0uphp57NVTeAGdJGaAliecxwBDHYQCIvrW7aDBZCYeNTP/WX69mkg==
@@ -3875,6 +3912,13 @@ saxes@^5.0.0:
38753912
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
38763913
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
38773914

3915+
3916+
version "7.3.4"
3917+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97"
3918+
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==
3919+
dependencies:
3920+
lru-cache "^6.0.0"
3921+
38783922
semver@^6.0.0, semver@^6.1.0, semver@^6.3.0:
38793923
version "6.3.0"
38803924
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
@@ -4318,6 +4362,23 @@ tr46@^2.0.2:
43184362
dependencies:
43194363
punycode "^2.1.1"
43204364

4365+
ts-jest@^26.4.4:
4366+
version "26.4.4"
4367+
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.4.4.tgz#61f13fb21ab400853c532270e52cc0ed7e502c49"
4368+
integrity sha512-3lFWKbLxJm34QxyVNNCgXX1u4o/RV0myvA2y2Bxm46iGIjKlaY0own9gIckbjZJPn+WaJEnfPPJ20HHGpoq4yg==
4369+
dependencies:
4370+
"@types/jest" "26.x"
4371+
bs-logger "0.x"
4372+
buffer-from "1.x"
4373+
fast-json-stable-stringify "2.x"
4374+
jest-util "^26.1.0"
4375+
json5 "2.x"
4376+
lodash.memoize "4.x"
4377+
make-error "1.x"
4378+
mkdirp "1.x"
4379+
semver "7.x"
4380+
yargs-parser "20.x"
4381+
43214382
tsconfig-paths@^3.9.0:
43224383
version "3.9.0"
43234384
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
@@ -4663,6 +4724,16 @@ y18n@^4.0.0:
46634724
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
46644725
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
46654726

4727+
yallist@^4.0.0:
4728+
version "4.0.0"
4729+
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
4730+
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
4731+
4732+
4733+
version "20.2.4"
4734+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54"
4735+
integrity sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==
4736+
46664737
yargs-parser@^18.1.2:
46674738
version "18.1.3"
46684739
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"

0 commit comments

Comments
 (0)