From 94d1c92d9b002ca727f048bd97d4dbb2e128227b Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 21 Feb 2022 16:04:20 +0100 Subject: [PATCH] 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 --- .github/workflows/check.yml | 14 ++++++ scripts/jest.config.ts | 8 +++ scripts/package.json | 5 +- scripts/release/__tests__/common.test.ts | 63 ++++++++++++++++++++++++ scripts/release/common.ts | 16 ++++++ scripts/release/process-release.ts | 15 +----- scripts/tsconfig.json | 1 + yarn.lock | 2 + 8 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 scripts/jest.config.ts create mode 100644 scripts/release/__tests__/common.test.ts diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index f61eec366a..8bfdfc0479 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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 diff --git a/scripts/jest.config.ts b/scripts/jest.config.ts new file mode 100644 index 0000000000..d2cc09a672 --- /dev/null +++ b/scripts/jest.config.ts @@ -0,0 +1,8 @@ +import type { Config } from '@jest/types'; + +const config: Config.InitialOptions = { + preset: 'ts-jest', + testEnvironment: 'node', +}; + +export default config; diff --git a/scripts/package.json b/scripts/package.json index cf73e43f4c..de616795ba 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -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", @@ -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" } } diff --git a/scripts/release/__tests__/common.test.ts b/scripts/release/__tests__/common.test.ts new file mode 100644 index 0000000000..fa261277f4 --- /dev/null +++ b/scripts/release/__tests__/common.test.ts @@ -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 + " + `); + }); +}); diff --git a/scripts/release/common.ts b/scripts/release/common.ts index 248b94e422..4d333a1635 100644 --- a/scripts/release/common.ts +++ b/scripts/release/common.ts @@ -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'); +} diff --git a/scripts/release/process-release.ts b/scripts/release/process-release.ts index 669c115c56..b0e9cfa8d7 100755 --- a/scripts/release/process-release.ts +++ b/scripts/release/process-release.ts @@ -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.'); } diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 20c3f4903a..34cc31a9fd 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../base.tsconfig.json", "compilerOptions": { "typeRoots": ["../node_modules/@types"], + "types": ["node", "jest"], "outDir": "dist" }, "include": ["pre-gen/setHostsOptions.ts", "release/*"], diff --git a/yarn.lock b/yarn.lock index 261f6d01fe..b3eb619639 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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