Skip to content

test(script): add jest for scripts package #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,17 @@ jobs:

- name: Run CTS
run: yarn cts:test

scripts:
runs-on: ubuntu-20.04
needs: setup
timeout-minutes: 20
steps:
- uses: actions/checkout@v2

- name: Restore cache
id: restore
uses: ./.github/actions/cache

- name: Test scripts
run: yarn workspace scripts test
8 changes: 8 additions & 0 deletions scripts/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
};

export default config;
5 changes: 4 additions & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"build": "tsc",
"createReleaseIssue": "yarn build && node dist/scripts/release/create-release-issue.js",
"processRelease": "yarn build && node dist/scripts/release/process-release.js",
"setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js"
"setHostsOptions": "yarn build && node dist/scripts/pre-gen/setHostsOptions.js",
"test": "jest"
},
"devDependencies": {
"@octokit/rest": "18.12.0",
Expand All @@ -14,8 +15,10 @@
"@types/semver": "7.3.9",
"dotenv": "16.0.0",
"execa": "5.1.1",
"jest": "27.4.7",
"js-yaml": "4.1.0",
"semver": "7.3.5",
"ts-jest": "27.1.3",
"typescript": "4.5.4"
}
}
63 changes: 63 additions & 0 deletions scripts/release/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getMarkdownSection } from '../common';

describe('getMarkdownSection', () => {
it('gets the section correctly', () => {
const text = `
# hello

hi

# world

hey
`;
expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
"# hello

hi
"
`);
});

it('gets the sub headings', () => {
const text = `
# hi

# hello

## sub-heading

hello

# this shouldn't be included

right?
`;

expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
"# hello

## sub-heading

hello
"
`);
});

it('gets the whole text till the end', () => {
const text = `
# hi

# hello

this is a test
`;

expect(getMarkdownSection(text, '# hello')).toMatchInlineSnapshot(`
"# hello

this is a test
"
`);
});
});
16 changes: 16 additions & 0 deletions scripts/release/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ export const LANGS = [
)
),
];

export function getMarkdownSection(markdown: string, title: string): string {
const levelIndicator = title.split(' ')[0]; // e.g. `##`
const lines = markdown
.slice(markdown.indexOf(title))
.split('\n')
.map((line) => line.trim());
let endIndex = lines.length;
for (let i = 1; i < lines.length; i++) {
if (lines[i].startsWith(`${levelIndicator} `)) {
endIndex = i;
break;
}
}
return lines.slice(0, endIndex).join('\n');
}
15 changes: 1 addition & 14 deletions scripts/release/process-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,11 @@ import execa from 'execa';

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

import { MAIN_BRANCH, OWNER, REPO, run } from './common';
import { MAIN_BRANCH, OWNER, REPO, run, getMarkdownSection } from './common';
import TEXT from './text';

dotenv.config();

function getMarkdownSection(markdown: string, title: string): string {
const levelIndicator = title.split(' ')[0]; // e.g. `##`
const lines = markdown.slice(markdown.indexOf(title)).split('\n');
let endIndex = lines.length;
for (let i = 1; i < lines.length; i++) {
if (lines[i].startsWith(`${levelIndicator} `)) {
endIndex = i;
break;
}
}
return lines.slice(0, endIndex).join('\n');
}

if (!process.env.GITHUB_TOKEN) {
throw new Error('Environment variable `GITHUB_TOKEN` does not exist.');
}
Expand Down
1 change: 1 addition & 0 deletions scripts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../base.tsconfig.json",
"compilerOptions": {
"typeRoots": ["../node_modules/@types"],
"types": ["node", "jest"],
"outDir": "dist"
},
"include": ["pre-gen/setHostsOptions.ts", "release/*"],
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9987,8 +9987,10 @@ __metadata:
"@types/semver": 7.3.9
dotenv: 16.0.0
execa: 5.1.1
jest: 27.4.7
js-yaml: 4.1.0
semver: 7.3.5
ts-jest: 27.1.3
typescript: 4.5.4
languageName: unknown
linkType: soft
Expand Down