Skip to content

Commit

Permalink
chore: added tests for expect outcomes
Browse files Browse the repository at this point in the history
  • Loading branch information
petertonysmith94 committed Jan 28, 2025
1 parent ec84b8a commit af123ca
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 24 deletions.
100 changes: 76 additions & 24 deletions packages/fuels/test/features/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import { existsSync, readFileSync } from 'fs';
import { existsSync } from 'fs';

import { Commands } from '../../src';
import { mockCheckForUpdates } from '../utils/mockCheckForUpdates';
Expand All @@ -10,6 +10,7 @@ import {
runInit,
resetDiskAndMocks,
resetConfigAndMocks,
loadFuelsConfig,
} from '../utils/runCommands';

/**
Expand Down Expand Up @@ -39,14 +40,14 @@ describe('init', () => {
});

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`workspace: './workspace',`);
expect(fuelsContents).toMatch(`output: './output',`);
expect(fuelsContents).not.toMatch(`forcPath: 'fuels-forc',`);
expect(fuelsContents).not.toMatch(`fuelCorePath: 'fuels-core',`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
workspace: './workspace',
output: './output',
});
});

it('should run `init` command with --contracts', async () => {
it('should run `init` command with --contracts [absolute path]', async () => {
await runInit({
root: paths.root,
contracts: [paths.contractsBarDir, paths.contractsFooDir],
Expand All @@ -59,10 +60,51 @@ describe('init', () => {
];

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/contracts:/);
expect(fuelsContents).toMatch(relativeBarDir);
expect(fuelsContents).toMatch(relativeFooDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
contracts: [relativeBarDir, relativeFooDir],
output: './output',
});
});

it('should run `init` command with --contracts [glob path - multiple matches]', async () => {
await runInit({
root: paths.root,
contracts: [`${paths.contractsDir}/*`],
output: paths.outputDir,
});

const relativeContractPaths = [
paths.upgradableChunkedContractPath,
paths.upgradableContractPath,
paths.contractsBarDir,
paths.contractsFooDir,
].map((path) => path.replace(paths.workspaceDir, 'workspace'));

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
contracts: expect.arrayContaining(relativeContractPaths),
output: './output',
});
});

it('should run `init` command with --contracts [glob path - single path]', async () => {
await runInit({
root: paths.root,
contracts: [`${paths.contractsBarDir}/*`],
output: paths.outputDir,
});

const [relativeBarDir] = [paths.contractsBarDir.replace(paths.workspaceDir, 'workspace')];

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();

const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@18

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@20

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25

Check failure on line 104 in packages/fuels/test/features/init.test.ts

View workflow job for this annotation

GitHub Actions / node@22

packages/fuels/test/features/init.test.ts > init > should run `init` command with --contracts [glob path - single path]

AssertionError: expected { contracts: [ …(2) ], …(1) } to deeply equal { …(2) } - Expected + Received Object { "contracts": Array [ - "workspace/contracts/bar", + "workspace/contracts/bar/src", + "workspace/contracts/bar/Forc.toml", ], "output": "./output", } ❯ packages/fuels/test/features/init.test.ts:104:25
contracts: [relativeBarDir],
output: './output',
});
});

it('should run `init` command with --predicates', async () => {
Expand All @@ -75,9 +117,11 @@ describe('init', () => {
const relativePredicateDir = paths.predicateDir.replace(paths.workspaceDir, 'workspace');

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/predicates:/);
expect(fuelsContents).toMatch(relativePredicateDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
predicates: [relativePredicateDir],
output: './output',
});
});

it('should run `init` command with --scripts', async () => {
Expand All @@ -90,9 +134,11 @@ describe('init', () => {
const relativeScriptDir = paths.scriptsDir.replace(paths.workspaceDir, 'workspace');

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(/scripts:/);
expect(fuelsContents).toMatch(relativeScriptDir);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
scripts: [relativeScriptDir],
output: './output',
});
});

it('should run `init` command using custom binaries', async () => {
Expand All @@ -105,11 +151,13 @@ describe('init', () => {
});

expect(existsSync(paths.fuelsConfigPath)).toBeTruthy();
const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`workspace: './workspace',`);
expect(fuelsContents).toMatch(`output: './output',`);
expect(fuelsContents).toMatch(`forcPath: 'fuels-forc',`);
expect(fuelsContents).toMatch(`fuelCorePath: 'fuels-core',`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
workspace: './workspace',
output: './output',
forcPath: 'fuels-forc',
fuelCorePath: 'fuels-core',
});
});

it('should run `init` command with custom fuel-core-port', async () => {
Expand All @@ -120,8 +168,12 @@ describe('init', () => {
fuelCorePort: '1234',
});

const fuelsContents = readFileSync(paths.fuelsConfigPath, 'utf-8');
expect(fuelsContents).toMatch(`fuelCorePort: 1234,`);
const fuelsConfig = await loadFuelsConfig(paths.fuelsConfigPath);
expect(fuelsConfig).toEqual({
fuelCorePort: 1234,
output: './output',
workspace: './workspace',
});
});

it('should run `init` command and throw for existent config file', async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/fuels/test/utils/runCommands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { join, basename } from 'path';

import type { FuelsConfig } from '../../src';
import { Commands } from '../../src';
import { run } from '../../src/run';

Expand Down Expand Up @@ -187,3 +188,12 @@ export function resetDiskAndMocks(dirPath: string) {
}
vi.restoreAllMocks();
}

/**
* Loaders
*/
export async function loadFuelsConfig(configPath: string): Promise<FuelsConfig> {
const configPathWithCacheBust = `${configPath}?update=${Date.now()}`;
const { default: fuelsConfig } = await import(configPathWithCacheBust);
return fuelsConfig;
}

0 comments on commit af123ca

Please sign in to comment.