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

Tests: run against compiled source rather than transpiling on the fly #851

Merged
merged 3 commits into from
Jan 4, 2025
Merged
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
"webpack-server": "grunt server",
"webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
"test": "npm run lint && npm run build && npm run webpack-build && npm run test:compose && npm run test:generated-types",
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC npm run mocha",
"test": "npm run build && npm run lint && npm run webpack-build && npm run test:compose && npm run test:generated-types",
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC npx mocha",
"test:generated-types": "npm run generate-types && git diff --exit-code ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts",
"mocha": "TS_NODE_FILES=true mocha",
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.ts",
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
},
Expand Down Expand Up @@ -69,6 +68,7 @@
},
"devDependencies": {
"@balena/lint": "^8.2.8",
"@balena/pinejs": "file:./",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I've used file:<path> locally for testing but not for anything like this.

"@faker-js/faker": "^9.3.0",
"@types/busboy": "^1.5.4",
"@types/chai": "^4.3.20",
Expand Down
3 changes: 1 addition & 2 deletions src/sbvr-api/express-extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Augment express.js with pinejs-specific attributes via declaration merging.
import type { User as PineUser } from './sbvr-utils';

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Express {
type PineUser = import('./sbvr-utils').User;

// Augment Express.User to include the props of our PineUser.
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface User extends PineUser {}
Expand Down
38 changes: 20 additions & 18 deletions test/03-async-migrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import supertest from 'supertest';
import type { ChildProcess } from 'child_process';
import { assert, expect } from 'chai';
import { setTimeout } from 'timers';
import { dbModule } from '../src/server-glue/module';
import type { Migrator } from '@balena/pinejs';
import { dbModule } from '@balena/pinejs';
import { testInit, testDeInit, testLocalServer } from './lib/test-init';
import type { MigrationStatus } from '../src/migrator/utils';

const fixturesBasePath = __dirname + '/fixtures/03-async-migrator/';

Expand Down Expand Up @@ -88,7 +88,9 @@ const waitForAllDataMigrated = async function (
return [result, allResults];
};

const getMigrationStatus = async function (): Promise<MigrationStatus[]> {
const getMigrationStatus = async function (): Promise<
Migrator.MigrationStatus[]
> {
const res = await supertest(testLocalServer).get(
'/migrations/migration_status',
);
Expand Down Expand Up @@ -142,7 +144,7 @@ describe('03 Async Migrations', function () {
.that.has.ownProperty('d')
.to.be.an('array');

res.body.d.map(async (migration: MigrationStatus) => {
res.body.d.map(async (migration: Migrator.MigrationStatus) => {
await supertest(testLocalServer)
.patch(`/migrations/migration_status('${migration.migration_key}')`)
.send({
Expand All @@ -153,7 +155,7 @@ describe('03 Async Migrations', function () {
});

it('should run one async migrator', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
while (result.length === 0 || result[0]?.migrated_row_count < 1) {
result = await getMigrationStatus();
Expand All @@ -165,7 +167,7 @@ describe('03 Async Migrations', function () {

it('should complete / catch up data in one async migrator', async function () {
// active wait to check if migrations have catched up
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
await waitForAllDataMigrated('device');
// active wait as updating migration status after migrations have done takes some time.
// dont want to wait for an artificial magic time delay.
Expand All @@ -177,7 +179,7 @@ describe('03 Async Migrations', function () {
});

it('should migrate future data change after first catch up', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];

const startTime = Date.now().valueOf();
// first catch up is precondition from above test case.
Expand Down Expand Up @@ -218,7 +220,7 @@ describe('03 Async Migrations', function () {
pineFirstInstace = await testInit({ configPath, deleteDb: false });

await delay(200); // wait for one to have happened
let result: MigrationStatus[] = await getMigrationStatus();
let result: Migrator.MigrationStatus[] = await getMigrationStatus();
for (const row of result) {
expect(row.run_count).to.be.equal(0);
}
Expand All @@ -239,7 +241,7 @@ describe('03 Async Migrations', function () {
pineFirstInstace = await testInit({ configPath, deleteDb: false });

await delay(200); // wait for one to have happened
const result: MigrationStatus[] = await getMigrationStatus();
const result: Migrator.MigrationStatus[] = await getMigrationStatus();
for (const row of result) {
expect(row.run_count).to.be.greaterThan(0);
}
Expand Down Expand Up @@ -307,7 +309,7 @@ describe('03 Async Migrations', function () {
});

it('should start 2 migrations competitive', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
while (
result.length < 2 ||
Expand All @@ -325,7 +327,7 @@ describe('03 Async Migrations', function () {
});

it('should complete / catch up all data in 2 migrations competitive', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
// active wait to check if migrations have catched up
await waitForAllDataMigrated('device');
await waitForAllDataMigrated('deviceb');
Expand All @@ -347,7 +349,7 @@ describe('03 Async Migrations', function () {
});

it('should migrate future data change after first catch up in 2 migrators', async function () {
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];
while (
result.length < 2 ||
result[0]?.is_backing_off === false ||
Expand Down Expand Up @@ -401,7 +403,7 @@ describe('03 Async Migrations', function () {
// It's meant to be a wait until we can 'surely' assume that the async migrations
// would have run at least one iteration. Still a magic number is undesired as it's error prone
await delay(2000); // wait for some migrations to have happened
let result: MigrationStatus[] = [];
let result: Migrator.MigrationStatus[] = [];

result = await getMigrationStatus();
expect(result).to.be.empty;
Expand Down Expand Up @@ -436,7 +438,7 @@ describe('03 Async Migrations', function () {
});

it('should report error in error count', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (rows.length < 2 || !rows[0]?.error_count || !rows[1]?.run_count) {
Expand All @@ -455,7 +457,7 @@ describe('03 Async Migrations', function () {
});

it('should switch to backoff when exceeding error threshold and give error message', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (
Expand All @@ -479,7 +481,7 @@ describe('03 Async Migrations', function () {
});

it('should remain in backoff when exceeding error threshold and give error message', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002', '0003'];
while (
Expand All @@ -504,7 +506,7 @@ describe('03 Async Migrations', function () {
});

it('should recover from error backoff when no migration error occurs and rows get migrated', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];
// active wait until 1 row has been migrated
const errorMigrationKeys = ['0002'];
while (rows.length === 0 || rows[0]?.error_count <= 5) {
Expand Down Expand Up @@ -580,7 +582,7 @@ describe('03 Async Migrations', function () {
});

it('should complete / catch up massive data in one async migrator', async function () {
let rows: MigrationStatus[] = [];
let rows: Migrator.MigrationStatus[] = [];

const res = await supertest(testLocalServer)
.get('/example/device/$count')
Expand Down
4 changes: 2 additions & 2 deletions test/07-permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import supertest from 'supertest';
import { expect } from 'chai';
const configPath = __dirname + '/fixtures/07-permissions/config.js';
import { testInit, testDeInit, testLocalServer } from './lib/test-init';
import { sbvrUtils, permissions } from '../src/server-glue/module';
import type UserModel from '../src/sbvr-api/user';
import { sbvrUtils, permissions } from '@balena/pinejs';
import type UserModel from '@balena/pinejs/out/sbvr-api/user';

describe('07 permissions tests', function () {
let pineServer: Awaited<ReturnType<typeof testInit>>;
Expand Down
12 changes: 6 additions & 6 deletions test/08-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { randomUUID } from 'node:crypto';
import { setTimeout } from 'node:timers/promises';
import { PineTest } from 'pinejs-client-supertest';
import { testInit, testDeInit, testLocalServer } from './lib/test-init';
import { tasks as tasksEnv } from '../src/config-loader/env';
import type Model from '../src/tasks/tasks';
import { env } from '@balena/pinejs';
import type Model from '@balena/pinejs/out/tasks/tasks';
import * as cronParser from 'cron-parser';
import { PINE_TEST_SIGNALS } from './lib/common';

Expand All @@ -17,7 +17,7 @@ export async function waitFor(checkFn: () => Promise<boolean>): Promise<void> {
const maxCount = 10;
for (let i = 1; i <= maxCount; i++) {
console.log(`Waiting (${i}/${maxCount})...`);
await setTimeout(tasksEnv.queueIntervalMS);
await setTimeout(env.tasks.queueIntervalMS);
if (await checkFn()) {
return;
}
Expand Down Expand Up @@ -229,8 +229,8 @@ describe('08 task tests', function () {
expect(created).to.be.lessThan(started);

// Calculate the earliest and latest start times based on queue interval
const earliest = scheduled - tasksEnv.queueIntervalMS;
const latest = scheduled + tasksEnv.queueIntervalMS;
const earliest = scheduled - env.tasks.queueIntervalMS;
const latest = scheduled + env.tasks.queueIntervalMS;

// Check if the start time was within the expected range
expect(started)
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('08 task tests', function () {
});

// Await more than the queue interval to ensure the task is not picked up
await setTimeout(2 * tasksEnv.queueIntervalMS);
await setTimeout(2 * env.tasks.queueIntervalMS);

await expectTask(pineTest, task.id, {
status: 'queued',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/00-basic/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/01-constrain/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'university';
const modelName = 'university';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/00-execute-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/01-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Migrator } from '../../../../src/server-glue/module';
import type { Migrator } from '@balena/pinejs';

const migration: Migrator.MigrationFn = async (tx) => {
const staticSql = `\
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/02-migrations-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Migrator } from '../../../../src/server-glue/module';
import type { Migrator } from '@balena/pinejs';

const migration: Migrator.MigrationFn = async (tx) => {
const staticSql = `\
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-sync-migrator/03-exclusive-category.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/02-sync-migrator/04-new-model-with-init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand All @@ -13,12 +13,12 @@ export default {
migrations: {
'0001': `
INSERT INTO "device" ("id", "name", "note", "type")
VALUES (2, 'no run', 'shouldNotRun', 'empty')
VALUES (2, 'no run', 'shouldNotRun', 'empty')
`,
},
initSql: `
INSERT INTO "device" ("id", "name", "note", "type")
VALUES (1, 'initName', 'shouldBeInit', 'init')
VALUES (1, 'initName', 'shouldBeInit', 'init')
`,
},
],
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/00-execute-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/01-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx) => {
const staticSql = `\
SELECT 1;`;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/02-parallel-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "deviceb"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/03-finalize-async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncFn: async (tx, options) => {
const staticSql = `\
UPDATE "device"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AsyncMigration } from '../../../../../src/migrator/utils';
import type { Migrator } from '@balena/pinejs';

const migration: AsyncMigration = {
const migration: Migrator.AsyncMigration = {
asyncSql: `\
UPDATE "deviceb"
SET "note" = "deviceb"."name"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/03-async-migrator/04-migration-errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigLoader } from '../../../src/server-glue/module';
import type { ConfigLoader } from '@balena/pinejs';

const apiRoot = 'example';
const modelName = 'example';
Expand Down
Loading
Loading