Skip to content

Commit

Permalink
Merge pull request #2 from piotr-oles/feature/improve-api
Browse files Browse the repository at this point in the history
feat: improve api
  • Loading branch information
piotr-oles authored Feb 24, 2021
2 parents 9b9e1a1 + fa55f42 commit 16e18ec
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 4,229 deletions.
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,41 @@ This package helps you with writing E2E tests for your packages.
Example:

```typescript
import { createSandbox, Sandbox } from "karton";
import {
createSandbox,
Sandbox,
packLocalPackage,
externalPackage,
Package
} from "karton";
import path from 'path';
import fs from 'fs-extra';

describe('my-package', () => {
let sandbox: Sandbox;
beforeEach(async () => {
let myPackage: Package;

beforeAll(async () => {
myPackage = await packLocalPackage(
path.resolve(__dirname, '../../')
);
sandbox = await createSandbox();
});
afterEach(async () => {
await sandbox.destroy();
await sandbox.reset();
});
afterAll(async () => {
await sandbox.cleanup();
await myPackage.remove();
})

it.each([
'^4.0.0',
'^5.0.0'
])('works with webpack %p', async (webpackVersion) => {
const packageJSON = JSON.parse(
await fs.readFile('fixtures/package.json', 'utf-8')
);
packageJSON.dependencies['webpack'] = webpackVersion;
packageJSON.dependencies['my-package'] = path.resolve(__dirname, '../my-package-0.0.0.tgz');

await sandbox.write('package.json', JSON.stringify(packageJSON));
await sandbox.write('src/test.js', await fs.readFile('fixtures/src/test.js'));

await sandbox.exec('yarn install');
externalPackage('webpack', '^4.0.0'),
externalPackage('webpack', '^5.0.0')
])('works with webpack %p', async (webpack) => {
await sandbox.load(path.join(__dirname, 'fixtures/basic'));
await sandbox.install('yarn', {
dependencies: [myPackage, webpack]
});
const result = await sandbox.exec('node src/test.js');

expect(result).toEqual('my-package awesome output');
Expand Down
40 changes: 40 additions & 0 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Logger } from "./logger";

function wait(timeout = 250) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}

interface RetryOptions {
retries?: number;
delay?: number;
}

/**
* The IO effects sometimes fail due to different external issues - for example, network or filesystem.
* To make these tests more reliable, we can wrap these effects in the `retry` function.
*/
async function retry<T>(
effect: () => Promise<T>,
logger: Logger,
options: RetryOptions = {}
): Promise<T> {
const retries = options.retries || 3;
const delay = options.delay || 250;

let lastError: unknown;

for (let retry = 1; retry <= retries; ++retry) {
try {
return await effect();
} catch (error) {
logger.log(error.toString());
logger.log(`Retry ${retry} of ${retries}.`);
lastError = error;
await wait(delay);
}
}

throw lastError;
}

export { wait, retry, RetryOptions };
Loading

0 comments on commit 16e18ec

Please sign in to comment.