Skip to content

Commit

Permalink
test(ci-context): migrate tests from jest to vitest (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor authored Dec 13, 2024
1 parent 48b0475 commit 823e4a0
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 276 deletions.
2 changes: 1 addition & 1 deletion packages/ci-context/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Run `nx build ci-context` to build the library.

## Running unit tests

Run `nx test ci-context` to execute the unit tests via [Jest](https://jestjs.io).
Run `nx test ci-context` to execute the unit tests via [Vitest](https://vitest.dev/).
2 changes: 1 addition & 1 deletion packages/ci-context/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = [
'@nx/dependency-checks': [
'error',
{
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}'],
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}', '{projectRoot}/vite.config.{js,ts,mjs,mts}'],
},
],
},
Expand Down
10 changes: 0 additions & 10 deletions packages/ci-context/jest.config.ts

This file was deleted.

11 changes: 9 additions & 2 deletions packages/ci-context/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"sourceRoot": "packages/ci-context/src",
"projectType": "library",
"tags": ["type:lib", "scope:nx-container"],
"// targets": "to see all targets run: nx show project ci-context --web",
"targets": {}
"targets": {
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../../coverage/packages/ci-context"
}
}
}
}
26 changes: 9 additions & 17 deletions packages/ci-context/src/lib/utils/azure-devops.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import { RunnerContext } from '../interfaces';
import * as devops from './azure-devops';
import { Azure } from './azure-devops';

describe('Azure DevOps Context', () => {
let restore: RestoreFn;
let context: RunnerContext;

beforeEach(() => {
restore = mockedEnv(
{
BUILD_SOURCEVERSION: 'devops-sha',
BUILD_SOURCEVERSIONAUTHOR: 'devops-actor',
AGENT_JOBNAME: 'devops-job',
BUILD_BUILDID: '40',
BUILD_REPOSITORY_URI: 'https://azure.com/gperdomor/nx-tools',
},
{ clear: true }
);
vi.stubEnv('BUILD_SOURCEVERSION', 'devops-sha');
vi.stubEnv('BUILD_SOURCEVERSIONAUTHOR', 'devops-actor');
vi.stubEnv('AGENT_JOBNAME', 'devops-job');
vi.stubEnv('BUILD_BUILDID', '40');
vi.stubEnv('BUILD_REPOSITORY_URI', 'https://azure.com/gperdomor/nx-tools');
});

afterEach(() => {
jest.restoreAllMocks();
restore();
vi.unstubAllEnvs();
});

describe('context', () => {
describe('When building pull requests', () => {
beforeEach(() => {
process.env['SYSTEM_PULLREQUEST_SOURCEBRANCH'] = 'refs/heads/devops-ref-slug';
process.env['SYSTEM_PULLREQUEST_PULLREQUESTID'] = '123';
vi.stubEnv('SYSTEM_PULLREQUEST_SOURCEBRANCH', 'refs/heads/devops-ref-slug');
vi.stubEnv('SYSTEM_PULLREQUEST_PULLREQUESTID', '123');
});

it('Should be take proper context values', async () => {
Expand All @@ -52,7 +44,7 @@ describe('Azure DevOps Context', () => {

describe('When building branches', () => {
beforeEach(() => {
process.env['BUILD_SOURCEBRANCH'] = 'refs/heads/devops-ref-slug';
vi.stubEnv('BUILD_SOURCEBRANCH', 'refs/heads/devops-ref-slug');
});

it('Should be take proper context values', async () => {
Expand Down
40 changes: 12 additions & 28 deletions packages/ci-context/src/lib/utils/bitbucket.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import { RunnerContext } from '../interfaces';
import * as bitbucket from './bitbucket';
import { BitBucket } from './bitbucket';

describe('CircleCI Context', () => {
let restore: RestoreFn;
describe('BitBucket Context', () => {
let context: RunnerContext;

beforeEach(() => {
restore = mockedEnv(
{
BITBUCKET_PR_ID: 'pr-id',
BITBUCKET_COMMIT: 'bitbucket-sha',
BITBUCKET_BRANCH: 'bitbucket-ref-slug',
BITBUCKET_STEP_TRIGGERER_UUID: 'bitbucket-actor-uuid',
BITBUCKET_STEP_UUID: 'bitbucket-job-uuid',
BITBUCKET_BUILD_NUMBER: '50',
BITBUCKET_REPO_FULL_NAME: 'gperdomor/nx-tools',
BITBUCKET_WORKSPACE: 'nx-tools',
BITBUCKET_GIT_HTTP_ORIGIN: 'https://bitbucket.org/gperdomor/nx-tools',
},
{ clear: true }
);
vi.stubEnv('BITBUCKET_PR_ID', 'pr-id');
vi.stubEnv('BITBUCKET_COMMIT', 'bitbucket-sha');
vi.stubEnv('BITBUCKET_BRANCH', 'bitbucket-ref-slug');
vi.stubEnv('BITBUCKET_STEP_TRIGGERER_UUID', 'bitbucket-actor-uuid');
vi.stubEnv('BITBUCKET_STEP_UUID', 'bitbucket-job-uuid');
vi.stubEnv('BITBUCKET_BUILD_NUMBER', '50');
vi.stubEnv('BITBUCKET_REPO_FULL_NAME', 'gperdomor/nx-tools');
vi.stubEnv('BITBUCKET_WORKSPACE', 'nx-tools');
vi.stubEnv('BITBUCKET_GIT_HTTP_ORIGIN', 'https://bitbucket.org/gperdomor/nx-tools');
});

afterEach(() => {
jest.restoreAllMocks();
restore();
vi.unstubAllEnvs();
});

describe('context', () => {
Expand Down Expand Up @@ -66,16 +58,8 @@ describe('CircleCI Context', () => {
});

describe('When git tag is present', () => {
let restore: RestoreFn;

beforeEach(() => {
restore = mockedEnv({
BITBUCKET_TAG: 'bitbucket-tag',
});
});

afterEach(() => {
restore();
vi.stubEnv('BITBUCKET_TAG', 'bitbucket-tag');
});

it('Should be take proper context values', async () => {
Expand Down
37 changes: 13 additions & 24 deletions packages/ci-context/src/lib/utils/circle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import { RunnerContext } from '../interfaces';
import * as circle from './circle';
import { Circle } from './circle';

describe('CircleCI Context', () => {
let restore: RestoreFn;
let context: RunnerContext;

beforeEach(() => {
restore = mockedEnv(
{
CIRCLECI: 'true',
CI_PULL_REQUEST: 'true',
CIRCLE_SHA1: 'circleci-sha',
CIRCLE_BRANCH: 'circleci-ref-slug',
CIRCLE_USERNAME: 'circleci-actor',
CIRCLE_JOB: 'circleci-job',
CIRCLE_BUILD_NUM: '30',
CIRCLE_REPOSITORY_URL: 'https://circle.com/gperdomor/nx-tools',
CIRCLE_PROJECT_REPONAME: 'nx-tools',
},
{ clear: true }
);
vi.stubEnv('CIRCLECI', 'true');
vi.stubEnv('CI_PULL_REQUEST', 'true');
vi.stubEnv('CIRCLE_SHA1', 'circleci-sha');
vi.stubEnv('CIRCLE_BRANCH', 'circleci-ref-slug');
vi.stubEnv('CIRCLE_USERNAME', 'circleci-actor');
vi.stubEnv('CIRCLE_JOB', 'circleci-job');
vi.stubEnv('CIRCLE_BUILD_NUM', '30');
vi.stubEnv('CIRCLE_REPOSITORY_URL', 'https://circle.com/gperdomor/nx-tools');
vi.stubEnv('CIRCLE_PROJECT_REPONAME', 'nx-tools');
});

afterEach(() => {
jest.restoreAllMocks();
restore();
vi.restoreAllMocks();
vi.unstubAllEnvs();
});

describe('context', () => {
Expand Down Expand Up @@ -66,16 +59,12 @@ describe('CircleCI Context', () => {
});

describe('When git tag is present', () => {
let restore: RestoreFn;

beforeEach(() => {
restore = mockedEnv({
CIRCLE_TAG: 'circleci-tag',
});
vi.stubEnv('CIRCLE_TAG', 'circleci-tag');
});

afterEach(() => {
restore();
vi.stubEnv('CIRCLE_TAG', '');
});

it('Should be take proper context values', async () => {
Expand Down
42 changes: 16 additions & 26 deletions packages/ci-context/src/lib/utils/drone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import mockedEnv, { RestoreFn } from 'mocked-env';
import { RunnerContext } from '../interfaces';
import * as drone from './drone';
import { Drone } from './drone';

describe('Drone Context', () => {
let restore: RestoreFn;
let context: RunnerContext;

beforeEach(() => {
restore = mockedEnv(
{
DRONE: 'true',
DRONE_BUILD_EVENT: 'drone-event-name',
DRONE_COMMIT_SHA: 'drone-sha',
DRONE_COMMIT_REF: 'refs/heads/drone-ref',
DRONE_STAGE_NAME: 'drone-job',
DRONE_COMMIT_AUTHOR: 'drone-actor',
DRONE_BUILD_NUMBER: '100',
DRONE_REPO_BRANCH: 'drone-main',
DRONE_REPO_LINK: 'https://drone.com/gperdomor/nx-tools',
DRONE_REPO: 'gperdomor/nx-tools',
},
{ clear: true }
);
vi.stubEnv('DRONE', 'true');
vi.stubEnv('DRONE_BUILD_EVENT', 'drone-event-name');
vi.stubEnv('DRONE_COMMIT_SHA', 'drone-sha');
vi.stubEnv('DRONE_COMMIT_REF', 'refs/heads/drone-ref');
vi.stubEnv('DRONE_STAGE_NAME', 'drone-job');
vi.stubEnv('DRONE_COMMIT_AUTHOR', 'drone-actor');
vi.stubEnv('DRONE_BUILD_NUMBER', '100');
vi.stubEnv('DRONE_REPO_BRANCH', 'drone-main');
vi.stubEnv('DRONE_REPO_LINK', 'https://drone.com/gperdomor/nx-tools');
vi.stubEnv('DRONE_REPO', 'gperdomor/nx-tools');
});

afterEach(() => {
jest.restoreAllMocks();
restore();
vi.restoreAllMocks();
vi.unstubAllEnvs();
});

describe('context', () => {
Expand All @@ -53,17 +46,14 @@ describe('Drone Context', () => {
});

describe('When git tag is present', () => {
let restore: RestoreFn;

beforeEach(() => {
restore = mockedEnv({
DRONE_COMMIT_REF: 'refs/tags/drone-v1.0.0',
DRONE_REPO_PRIVATE: 'true',
});
vi.stubEnv('DRONE_COMMIT_REF', 'refs/tags/drone-v1.0.0');
vi.stubEnv('DRONE_REPO_PRIVATE', 'true');
});

afterEach(() => {
restore();
vi.stubEnv('DRONE_COMMIT_REF', '');
vi.stubEnv('DRONE_REPO_PRIVATE', '');
});

it('Should be take proper context values', async () => {
Expand Down
Loading

0 comments on commit 823e4a0

Please sign in to comment.