Skip to content

Commit 1fa88dc

Browse files
authored
test(script): add jest for scripts package (#148)
* test(scripts): add jest for scripts package * chore: update jest config * chore(ci): run test on CI * chore(ci): run test after setup job
1 parent 4e63891 commit 1fa88dc

File tree

8 files changed

+109
-15
lines changed

8 files changed

+109
-15
lines changed

.github/workflows/check.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,17 @@ jobs:
230230

231231
- name: Run CTS
232232
run: yarn cts:test
233+
234+
scripts:
235+
runs-on: ubuntu-20.04
236+
needs: setup
237+
timeout-minutes: 20
238+
steps:
239+
- uses: actions/checkout@v2
240+
241+
- name: Restore cache
242+
id: restore
243+
uses: ./.github/actions/cache
244+
245+
- name: Test scripts
246+
run: yarn workspace scripts test

scripts/jest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Config } from '@jest/types';
2+
3+
const config: Config.InitialOptions = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'node',
6+
};
7+
8+
export default config;

scripts/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"build": "tsc",
66
"createReleaseIssue": "yarn build && node dist/scripts/release/create-release-issue.js",
77
"processRelease": "yarn build && node dist/scripts/release/process-release.js",
8-
"setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js"
8+
"setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js",
9+
"test": "jest"
910
},
1011
"devDependencies": {
1112
"@octokit/rest": "18.12.0",
@@ -14,8 +15,10 @@
1415
"@types/semver": "7.3.9",
1516
"dotenv": "16.0.0",
1617
"execa": "5.1.1",
18+
"jest": "27.4.7",
1719
"js-yaml": "4.1.0",
1820
"semver": "7.3.5",
21+
"ts-jest": "27.1.3",
1922
"typescript": "4.5.4"
2023
}
2124
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { getMarkdownSection } from '../common';
2+
3+
describe('getMarkdownSection', () => {
4+
it('gets the section correctly', () => {
5+
const text = `
6+
# hello
7+
8+
hi
9+
10+
# world
11+
12+
hey
13+
`;
14+
expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
15+
"# hello
16+
17+
hi
18+
"
19+
`);
20+
});
21+
22+
it('gets the sub headings', () => {
23+
const text = `
24+
# hi
25+
26+
# hello
27+
28+
## sub-heading
29+
30+
hello
31+
32+
# this shouldn't be included
33+
34+
right?
35+
`;
36+
37+
expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
38+
"# hello
39+
40+
## sub-heading
41+
42+
hello
43+
"
44+
`);
45+
});
46+
47+
it('gets the whole text till the end', () => {
48+
const text = `
49+
# hi
50+
51+
# hello
52+
53+
this is a test
54+
`;
55+
56+
expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
57+
"# hello
58+
59+
this is a test
60+
"
61+
`);
62+
});
63+
});

scripts/release/common.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ export const LANGS = [
3636
)
3737
),
3838
];
39+
40+
export function getMarkdownSection(markdown: string, title: string): string {
41+
const levelIndicator = title.split(' ')[0]; // e.g. `##`
42+
const lines = markdown
43+
.slice(markdown.indexOf(title))
44+
.split('\n')
45+
.map((line) => line.trim());
46+
let endIndex = lines.length;
47+
for (let i = 1; i < lines.length; i++) {
48+
if (lines[i].startsWith(`${levelIndicator} `)) {
49+
endIndex = i;
50+
break;
51+
}
52+
}
53+
return lines.slice(0, endIndex).join('\n');
54+
}

scripts/release/process-release.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,11 @@ import execa from 'execa';
66

77
import openapitools from '../../openapitools.json';
88

9-
import { MAIN_BRANCH, OWNER, REPO, run } from './common';
9+
import { MAIN_BRANCH, OWNER, REPO, run, getMarkdownSection } from './common';
1010
import TEXT from './text';
1111

1212
dotenv.config();
1313

14-
function getMarkdownSection(markdown: string, title: string): string {
15-
const levelIndicator = title.split(' ')[0]; // e.g. `##`
16-
const lines = markdown.slice(markdown.indexOf(title)).split('\n');
17-
let endIndex = lines.length;
18-
for (let i = 1; i < lines.length; i++) {
19-
if (lines[i].startsWith(`${levelIndicator} `)) {
20-
endIndex = i;
21-
break;
22-
}
23-
}
24-
return lines.slice(0, endIndex).join('\n');
25-
}
26-
2714
if (!process.env.GITHUB_TOKEN) {
2815
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
2916
}

scripts/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../base.tsconfig.json",
33
"compilerOptions": {
44
"typeRoots": ["../node_modules/@types"],
5+
"types": ["node", "jest"],
56
"outDir": "dist"
67
},
78
"include": ["pre-gen/setHostsOptions.ts", "release/*"],

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9987,8 +9987,10 @@ __metadata:
99879987
"@types/semver": 7.3.9
99889988
dotenv: 16.0.0
99899989
execa: 5.1.1
9990+
jest: 27.4.7
99909991
js-yaml: 4.1.0
99919992
semver: 7.3.5
9993+
ts-jest: 27.1.3
99929994
typescript: 4.5.4
99939995
languageName: unknown
99949996
linkType: soft

0 commit comments

Comments
 (0)