Skip to content

Commit

Permalink
chore: improving tests on versions package (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
arboleya authored May 8, 2023
1 parent b71ad9f commit 49c103b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .changeset/tricky-bears-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
34 changes: 33 additions & 1 deletion packages/versions/scripts/rewriteVersions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { readVersions, readVersionsFromEnv, readVersionsFromFiles } from './rewriteVersions';
import * as fsMod from 'fs';

import {
readVersions,
readVersionsFromEnv,
readVersionsFromFiles,
rewriteVersions,
} from './rewriteVersions';

// https://stackoverflow.com/a/72885576
jest.mock('fs', () => ({
__esModule: true,
...jest.requireActual('fs'),
}));

describe('rewriteVersions.js', () => {
function modifyEnv() {
Expand Down Expand Up @@ -72,4 +85,23 @@ describe('rewriteVersions.js', () => {
expect(versions.FUEL_CORE).toEqual(versionsFromFiles.FUEL_CORE);
expect(versions.FUELS).toEqual(versionsFromFiles.FUELS);
});

test('should rewrite files', () => {
// mocking
const writeFileSync = jest.spyOn(fsMod, 'writeFileSync').mockImplementation();

// executing
rewriteVersions();

// validating
const versions = readVersions();
const { lastCall } = writeFileSync.mock;

expect(writeFileSync).toHaveBeenCalledTimes(1);

expect(lastCall?.[0]).toMatch(/packages\/versions\/src\/lib\/getSupportedVersions\.ts/);
expect(lastCall?.[1]).toMatch(new RegExp(`FORC: '${versions.FORC}'`));
expect(lastCall?.[1]).toMatch(new RegExp(`FUEL_CORE: '${versions.FUEL_CORE}'`));
expect(lastCall?.[1]).toMatch(new RegExp(`FUELS: '${versions.FUELS}'`));
});
});
1 change: 1 addition & 0 deletions packages/versions/scripts/rewriteVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const rewriteVersions = () => {
writeFileSync(filepath, contents);
};

/* istanbul ignore next */
// Do not auto-run script when inside jest runner
if (!/jest\.js$/m.test(process.argv[1])) {
rewriteVersions();
Expand Down
10 changes: 8 additions & 2 deletions packages/versions/src/lib/getUserVersions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as execSyncProxyMod from '../proxies/execSync';
import * as childProcessMod from 'child_process';

import { getUserVersions } from './getUserVersions';

// https://stackoverflow.com/a/72885576
jest.mock('child_process', () => ({
__esModule: true,
...jest.requireActual('child_process'),
}));

describe('getUserVersions.js', () => {
/*
Test (mocking) utility
Expand All @@ -24,7 +30,7 @@ describe('getUserVersions.js', () => {
});

const execSync = jest
.spyOn(execSyncProxyMod, 'execSync')
.spyOn(childProcessMod, 'execSync')
.mockImplementation(shouldThrow ? execSyncThrow : mockedExecOk);

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/versions/src/lib/getUserVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execSync } from '../proxies/execSync';
import { execSync } from 'child_process';

export function getUserVersions(params: { fuelUpLink: string }) {
const { error } = console;
Expand Down
3 changes: 0 additions & 3 deletions packages/versions/src/proxies/execSync.ts

This file was deleted.

0 comments on commit 49c103b

Please sign in to comment.