Skip to content

Commit

Permalink
test(e2e): add esm tests (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsndr authored Jun 9, 2024
1 parent 045c2dd commit ab03b66
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
14 changes: 5 additions & 9 deletions e2e/yearly.e2e.spec.ts → e2e/commonjs.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { DateTime, Frequency, Month, RRule, RRuleSet } from '../src';
import { Sandbox } from './sandbox/sandbox';

describe('Yearly', () => {
const sandbox = new Sandbox();
describe('Commonjs', () => {
const sandbox = new Sandbox({
esm: false,
});

beforeAll(() => {
const version = process.env['E2E_LIBRARY_VERSION'];

if (!version) {
throw new Error('E2E_LIBRARY_VERSION is required');
}

sandbox.install(version);
sandbox.install();
});

afterAll(() => {
Expand Down
52 changes: 52 additions & 0 deletions e2e/esm.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { DateTime, Frequency, Month, RRule, RRuleSet } from '../src';
import { Sandbox } from './sandbox/sandbox';

describe('ESM', () => {
const sandbox = new Sandbox({
esm: true,
});

beforeAll(() => {
sandbox.install();
});

afterAll(() => {
sandbox.uninstall();
});

it('yearly in June and July for 10 occurrences', () => {
const result = sandbox.run(() => {
const rrule = new RRule({
frequency: Frequency.Yearly,
byMonth: [Month.June, Month.July],
count: 10,
});
const set = new RRuleSet(
DateTime.create(1997, 6, 10, 9, 0, 0, false),
'US/Eastern',
).addRrule(rrule);

return {
asString: set.toString(),
dates: set.all(),
};
});

expect(result).toEqual({
asString:
'DTSTART;TZID=US/Eastern:19970610T090000\nRRULE:FREQ=YEARLY;COUNT=10;BYMONTH=6,7',
dates: [
DateTime.create(1997, 6, 10, 9, 0, 0, false),
DateTime.create(1997, 7, 10, 9, 0, 0, false),
DateTime.create(1998, 6, 10, 9, 0, 0, false),
DateTime.create(1998, 7, 10, 9, 0, 0, false),
DateTime.create(1999, 6, 10, 9, 0, 0, false),
DateTime.create(1999, 7, 10, 9, 0, 0, false),
DateTime.create(2000, 6, 10, 9, 0, 0, false),
DateTime.create(2000, 7, 10, 9, 0, 0, false),
DateTime.create(2001, 6, 10, 9, 0, 0, false),
DateTime.create(2001, 7, 10, 9, 0, 0, false),
],
});
});
});
38 changes: 34 additions & 4 deletions e2e/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { rimrafSync } from 'rimraf';
import * as path from 'path';

export type SandboxOptions = {
esm: boolean;
};

export class Sandbox {
private readonly projectPath: string = path.resolve(__dirname, 'app');
private readonly projectPath: string;
private readonly esm: boolean;
constructor(options: SandboxOptions) {
this.projectPath = path.resolve(__dirname, 'app');
this.esm = options.esm;
}

install(version: string) {
install() {
rimrafSync(path.resolve(this.projectPath));
mkdirSync(this.projectPath);
execSync(`npm init -y`, { cwd: this.projectPath });
execSync(`npm install rrule-rust@${version}`, { cwd: this.projectPath });

if (this.esm) {
execSync(`npm pkg set type=module`, { cwd: this.projectPath });
}

execSync(`npm install rrule-rust@${this.getVersion()}`, {
cwd: this.projectPath,
});
}

uninstall() {
Expand All @@ -21,7 +37,11 @@ export class Sandbox {
writeFileSync(
path.resolve(this.projectPath, 'index.js'),
`
const src_1 = require('rrule-rust');
${
this.esm
? `import * as src_1 from 'rrule-rust';`
: `const src_1 = require('rrule-rust');`
}
const code = ${code.toString()};
Expand All @@ -37,4 +57,14 @@ export class Sandbox {
rmSync(path.resolve(this.projectPath, 'index.js'));
}
}

private getVersion() {
const version = process.env['E2E_LIBRARY_VERSION'];

if (!version) {
throw new Error('E2E_LIBRARY_VERSION is required');
}

return version;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"postbuild": "cpy ./src/lib/* ./dist/lib --flat",
"prepublishOnly": "napi prepublish -t npm",
"test": "cross-env TZ=Europe/Moscow jest --testPathIgnorePatterns=\\.e2e\\.spec\\.ts",
"test:e2e": "jest --testMatch='**/*.e2e.spec.ts'",
"test:e2e": "jest --testMatch='**/*.e2e.spec.ts' --runInBand",
"universal": "napi universal",
"version": "napi version",
"benchmark": "ts-node ./benchmark/index.ts",
Expand Down

0 comments on commit ab03b66

Please sign in to comment.