Skip to content
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

Add configurable test timeouts #73

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/steps/inject-root-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('#injectRootPackage()', () => {
testInParallel: true,
compileScriptName: 'compile',
printSuccessfulOutput: false,
timeouts: { test: 30000 },
}));
const tmp = require('tmp') as jest.Mocked<typeof tmpDef>;

Expand Down
9 changes: 8 additions & 1 deletion src/steps/test-projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('#testProjects()', () => {
testInParallel: true,
compileScriptName: 'compile',
printSuccessfulOutput: false,
timeouts: { test: 30000 },
}));

const packageManager = (require('../utils/package-manager') as {
Expand Down Expand Up @@ -88,7 +89,12 @@ describe('#testProjects()', () => {

await expect(testProjects(['/example-project'], finalResult)).resolves.not.toThrow();
expect(execa).toHaveBeenCalledTimes(1);
expect(execa).toHaveBeenCalledWith('yarn', [], { cwd: '/example-project', all: true, reject: false });
expect(execa).toHaveBeenCalledWith('yarn', [], {
cwd: '/example-project',
all: true,
reject: false,
timeout: 30000,
});
expect(finalResult.successfulTests).toStrictEqual([]);
expect(finalResult.failedTests).toStrictEqual([]);
});
Expand All @@ -101,6 +107,7 @@ describe('#testProjects()', () => {
testInParallel: true,
compileScriptName: 'compile',
printSuccessfulOutput: true,
timeouts: { test: 30000 },
}));

const testProjects = (require('./test-projects') as { default: typeof testProjectsDef }).default;
Expand Down
3 changes: 2 additions & 1 deletion src/steps/test-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import printHeader from '../utils/print-header';
/** Installs dependencies into the root project and test projects */
export default async function testProjects(testProjectPaths: string[], finalResult: FinalResult): Promise<void> {
// Get the configuration for testing in parallel
const { testInParallel, printSuccessfulOutput } = await getConfig();
const { testInParallel, printSuccessfulOutput, timeouts } = await getConfig();

// Print section header
printHeader('Testing projects');
Expand All @@ -29,6 +29,7 @@ export default async function testProjects(testProjectPaths: string[], finalResu
cwd: testProjectPath,
all: true,
reject: false,
timeout: timeouts.test,
});

// Check for failure
Expand Down
47 changes: 47 additions & 0 deletions src/utils/get-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,51 @@ describe('#getConfig()', () => {
'In the rugged.config.js file, the yarnMutexPort key contains an invalid value'
);
});

it('Handles invalid timeouts configuration', async () => {
jest
.spyOn(process, 'cwd')
.mockReturnValue(path.join(testFileTreesPath, 'invalid-config-value', 'timeouts-is-not-object'));

await expect(() => getConfig(true)).rejects.toThrow(PrintableError);
await expect(() => getConfig(true)).rejects.toThrow(
'In the rugged.config.js file, the timeouts key contains an invalid value'
);

jest
.spyOn(process, 'cwd')
.mockReturnValue(path.join(testFileTreesPath, 'invalid-config-value', 'timeouts-is-null'));

await expect(() => getConfig(true)).rejects.toThrow(PrintableError);
await expect(() => getConfig(true)).rejects.toThrow(
'In the rugged.config.js file, the timeouts key contains an invalid value'
);

jest
.spyOn(process, 'cwd')
.mockReturnValue(path.join(testFileTreesPath, 'invalid-config-value', 'timeouts-is-array'));

await expect(() => getConfig(true)).rejects.toThrow(PrintableError);
await expect(() => getConfig(true)).rejects.toThrow(
'In the rugged.config.js file, the timeouts key contains an invalid value'
);

jest
.spyOn(process, 'cwd')
.mockReturnValue(path.join(testFileTreesPath, 'invalid-config-value', 'timeouts-value-not-integer'));

await expect(() => getConfig(true)).rejects.toThrow(PrintableError);
await expect(() => getConfig(true)).rejects.toThrow(
'In the rugged.config.js file, the timeouts key contains an invalid value'
);

jest
.spyOn(process, 'cwd')
.mockReturnValue(path.join(testFileTreesPath, 'invalid-config-value', 'timeouts-value-not-positive-integer'));

await expect(() => getConfig(true)).rejects.toThrow(PrintableError);
await expect(() => getConfig(true)).rejects.toThrow(
'In the rugged.config.js file, the timeouts key contains an invalid value'
);
});
});
14 changes: 14 additions & 0 deletions src/utils/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export interface Config {

/** Whether to print successful test output, in addition to failed test output. Defaults to `false`. */
printSuccessfulOutput: boolean;

/** How long to wait before timing out, values are represented in milliseconds. Defaults to thirty seconds. */
timeouts: {
// Time before the test command times out.
test: number;
};
}

// Initialize
Expand All @@ -45,6 +51,9 @@ export default async function getConfig(reconstruct?: true): Promise<Config> {
testInParallel: true,
compileScriptName: 'compile',
printSuccessfulOutput: false,
timeouts: {
test: 30000,
},
};

// Initialize custom config
Expand All @@ -62,6 +71,11 @@ export default async function getConfig(reconstruct?: true): Promise<Config> {
testInParallel: (value) => typeof value === 'boolean',
compileScriptName: (value) => typeof value === 'string',
printSuccessfulOutput: (value) => typeof value === 'boolean',
timeouts: (value) =>
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
Object.values(value).every((timeout) => Number.isInteger(timeout) && Math.sign(timeout) !== -1),
};

// Initialize paths to possible config files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
timeouts: [],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
timeouts: 0,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
timeouts: null,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
timeouts: null,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
timeouts: null,
};