Skip to content

Commit

Permalink
Update jest, setup @edge-runtime/jest-environment
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Sep 11, 2023
1 parent 18c4924 commit 1acc0aa
Show file tree
Hide file tree
Showing 12 changed files with 1,408 additions and 1,260 deletions.
16 changes: 3 additions & 13 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,26 @@ module.exports = {
displayName: 'Node',
coverageDirectory: "coverage",
coveragePathIgnorePatterns: ["node_modules/", "dist/"],
globals: {
"ts-jest": {
tsConfig: "tsconfig.json"
}
},
moduleDirectories: ["node_modules"],
moduleFileExtensions: ["ts", "js"],
testEnvironment: "node",
testMatch: ["<rootDir>/packages/**/*.test.ts"],
testPathIgnorePatterns: ["/node_modules/", "/dist/", "/packages/edge/"],
transform: {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": ["ts-jest", "tsconfig.json"]
},
},
{
displayName: 'Edge',
coverageDirectory: "coverage",
coveragePathIgnorePatterns: ["node_modules/", "dist/"],
globals: {
"ts-jest": {
tsConfig: "tsconfig.json"
}
},
moduleDirectories: ["node_modules"],
moduleFileExtensions: ["ts", "js"],
testEnvironment: "node", // TODO: switch to @edge-runtime/jest-environment
testEnvironment: "@edge-runtime/jest-environment",
testMatch: ["<rootDir>/packages/edge/**/*.test.ts"],
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
transform: {
"^.+\\.ts$": "ts-jest"
"^.+\\.ts$": ["ts-jest", "tsconfig.json"]
},
},
]
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
"test": "jest"
},
"devDependencies": {
"@types/babel__traverse": "7.0.4",
"@edge-runtime/jest-environment": "^2.3.1",
"@types/babel__core": "7.0.4",
"@types/jest": "^24.0.17",
"@types/babel__traverse": "7.0.4",
"@types/jest": "^29.5.4",
"@types/node": "^12.7.2",
"jest": "^24.9.0",
"jest": "^29.6.4",
"lerna": "^3.16.4",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"ts-jest": "^29.1.1",
"ts-node": "^8.3.0",
"typescript": "^4.9.5"
}
Expand Down
8 changes: 2 additions & 6 deletions packages/browser/src/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("browser tests", () => {
// expect(actualValue).toEqual(expectedValue);
// });

it("should echo log if logtail sends 20x status code", async done => {
it("should echo log if logtail sends 20x status code", async () => {
nock("https://in.logtail.com")
.post("/")
.reply(201);
Expand All @@ -35,11 +35,9 @@ describe("browser tests", () => {
const browser = new Browser("valid source token");
const echoedLog = await browser.log(message);
expect(echoedLog.message).toEqual(expectedLog.message);

done();
});

it("should throw error if logtail sends non 200 status code", async done => {
it("should throw error if logtail sends non 200 status code", async () => {
nock("https://in.logtail.com")
.post("/")
.reply(401);
Expand All @@ -50,7 +48,5 @@ describe("browser tests", () => {
});
const message: string = String(Math.random);
await expect(browser.log(message)).rejects.toThrow();

done();
});
});
37 changes: 16 additions & 21 deletions packages/bunyan/src/bunyan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ function createLogger(logtail: Logtail): bunyan {
*
* @param level LogLevelString - Bunyan log level (string)
* @param logLevel LogLevel - Logtail log level
* @param cb Function - Callback to execute to signal test completion
*/
async function testLevel(
level: LogLevelString,
logLevel: LogLevel,
cb: Function,
) {
// Logtail fixtures
const logtail = new Logtail("test", { batchInterval: 1 });
Expand All @@ -51,9 +49,6 @@ async function testLevel(
// Log level should be 'info'
expect(logs[0].level).toBe(logLevel);

// Signal that the test has finished
setImmediate(() => cb());

return logs;
});

Expand All @@ -65,27 +60,27 @@ async function testLevel(
}

describe("Bunyan tests", () => {
it("should log at the 'debug' level", async done => {
return testLevel("debug", LogLevel.Debug, done);
it("should log at the 'debug' level", async () => {
return testLevel("debug", LogLevel.Debug);
});

it("should log at the 'info' level", async done => {
return testLevel("info", LogLevel.Info, done);
it("should log at the 'info' level", async () => {
return testLevel("info", LogLevel.Info);
});

it("should log at the 'warn' level", async done => {
return testLevel("warn", LogLevel.Warn, done);
it("should log at the 'warn' level", async () => {
return testLevel("warn", LogLevel.Warn);
});

it("should log at the 'error' level", async done => {
return testLevel("error", LogLevel.Error, done);
it("should log at the 'error' level", async () => {
return testLevel("error", LogLevel.Error);
});

it("should log at the 'fatal' level", async done => {
return testLevel("fatal", LogLevel.Fatal, done);
it("should log at the 'fatal' level", async () => {
return testLevel("fatal", LogLevel.Fatal);
});

it("should log using number levels", async done => {
it("should log using number levels", async () => {
// Fixtures
const levels: LevelTest[] = [
[20, LogLevel.Debug, "debug"],
Expand All @@ -102,7 +97,7 @@ describe("Bunyan tests", () => {

logtail.setSync(async logs => {
expect(logs.length).toBe(levels.length);
done();

return logs;
});

Expand All @@ -113,26 +108,26 @@ describe("Bunyan tests", () => {
levels.forEach(level => logger[level[2]](message));
});

it("should include arbitrary extra data fields", async done => {
it("should include arbitrary extra data fields", async () => {
const logtail = new Logtail("test");
logtail.setSync(async logs => {
expect(logs).toHaveLength(1);
expect(logs[0].message).toEqual("i am the message");
expect(logs[0].foo).toEqual("bar");
expect(logs[0].some).toEqual({ nested: "stuff" });
done();

return logs;
});
const logger = createLogger(logtail);
logger.info({ foo: "bar", some: { nested: "stuff" } }, "i am the message");
});

it("should include correct context fields", async done => {
it("should include correct context fields", async () => {
const logtail = new Logtail("test");
logtail.setSync(async logs => {
const context = logs[0].context;
expect(context.runtime.file).toMatch("bunyan.test.ts");
done();

return logs;
});
const logger = createLogger(logtail);
Expand Down
4 changes: 1 addition & 3 deletions packages/edge/src/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("edge tests", () => {
await logtail.log(message);
});

it("should pipe logs to a writable file stream", async done => {
it("should pipe logs to a writable file stream", async () => {
// Create a temporary file name
const temp = path.join(os.tmpdir(), `logtail_${Math.random()}`);

Expand Down Expand Up @@ -124,8 +124,6 @@ describe("edge tests", () => {
for (let i = 0; i < messages.length; i++) {
expect(data[i].message).toEqual(messages[i]);
}

done();
});

writeStream.end();
Expand Down
38 changes: 19 additions & 19 deletions packages/edge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"

"@logtail/core@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@logtail/core/-/core-0.4.0.tgz#a8e510756bf9fd1b2cc6c901225eb03685919ca8"
integrity sha512-xItEU5vY2LvcJyAVkxKKZ3UvG3CsK2pMvhuLDntlC+US1LlNcfQmVzvInN5j5iXl0kjNO6n+0EY2YyhhOgjWCQ==
"@logtail/core@^0.4.6":
version "0.4.9"
resolved "https://registry.yarnpkg.com/@logtail/core/-/core-0.4.9.tgz#78f2aeaacf02e6a34c93d859694341104d26e232"
integrity sha512-f9Dgt84OiLsn6lEzF7eCi9ND2ZPrY6jdmbDA55t0i5H5EfE7sehIgB6oof5YqPF9TB/p72sNoxKi5FDAb5iDrw==
dependencies:
"@logtail/tools" "^0.4.0"
"@logtail/types" "^0.4.0"
"@logtail/tools" "^0.4.9"
"@logtail/types" "^0.4.9"

"@logtail/tools@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@logtail/tools/-/tools-0.4.0.tgz#ff1bed193a005b12007df510f6ce294b8f518207"
integrity sha512-7lSKdJTq7NMUvriMhkf6cTxY9QlXi+YLLuHxbSOKI56HpJN8qJGPVcmOdzlbk8pJofe9RnME8hBMLTHAD3bmsw==
"@logtail/tools@^0.4.9":
version "0.4.9"
resolved "https://registry.yarnpkg.com/@logtail/tools/-/tools-0.4.9.tgz#c234ba144af68a8daae7b321b7239564918edea8"
integrity sha512-JfLG01JARLNmbCvrqGdQ7c7O1cVTNWLWgJ81+mQ/ZtlQVTJezVH+uQHwa2Qcez1vKOYOlgbL0s1/xJm0QJPKHQ==
dependencies:
"@logtail/types" "^0.4.0"
"@logtail/types" "^0.4.9"

"@logtail/types@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@logtail/types/-/types-0.4.0.tgz#39c28e08f805657c70107071dc166866153563a8"
integrity sha512-2CR3w7Xf5rKWbUlgiaBKeVrqWuWnVBz0Ymycw/gYwupVI0fb6Ameid9fHfeM5LI/gbIO3ZTIMADlA0FvFbuXMQ==
"@logtail/types@^0.4.6", "@logtail/types@^0.4.9":
version "0.4.9"
resolved "https://registry.yarnpkg.com/@logtail/types/-/types-0.4.9.tgz#5261204e5a2296c6c1edd57db116434a5944ebe6"
integrity sha512-3tKLic8gV93URDu0dB1kxmxGAK7SpnjdZx7/c3haELMsO64Cq4nlrwIVJNx/5g3Yd2G0RLbt7pvFPFfgQpqljA==
dependencies:
js "^0.1.0"

Expand Down Expand Up @@ -636,10 +636,10 @@ type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typescript@^3.9.7:
version "3.9.10"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.10.tgz#70f3910ac7a51ed6bef79da7800690b19bf778b8"
integrity sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==
typescript@^4.9.5:
version "4.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==

unbox-primitive@^1.0.0:
version "1.0.0"
Expand Down
30 changes: 6 additions & 24 deletions packages/koa/src/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function getServer(): [KoaLogtail, Koa] {
}

describe("Koa Logtail tests", () => {
it("should log successfully in Koa middleware", async done => {
it("should log successfully in Koa middleware", async () => {
const [logtail, koa] = getServer();

// Mock out the sync method
Expand All @@ -55,9 +55,6 @@ describe("Koa Logtail tests", () => {
// Should be 'info' level
expect(logs[0].level).toBe(LogLevel.Info);

// Finish task
await done();

return logs;
});

Expand All @@ -66,7 +63,7 @@ describe("Koa Logtail tests", () => {
.expect(200);
});

it("should log at 'warn' level when status is 401", async done => {
it("should log at 'warn' level when status is 401", async () => {
const [logtail, koa] = getServer();

// Mock out the sync method
Expand All @@ -80,9 +77,6 @@ describe("Koa Logtail tests", () => {
// Should be 'warn' level
expect(logs[0].level).toBe(LogLevel.Warn);

// Finish task
await done();

return logs;
});

Expand All @@ -91,7 +85,7 @@ describe("Koa Logtail tests", () => {
.expect(401);
});

it("should log at 'warn' level when status is 404", async done => {
it("should log at 'warn' level when status is 404", async () => {
const [logtail, koa] = getServer();

// Mock out the sync method
Expand All @@ -105,9 +99,6 @@ describe("Koa Logtail tests", () => {
// Should be 'warn' level
expect(logs[0].level).toBe(LogLevel.Warn);

// Finish task
await done();

return logs;
});

Expand All @@ -116,7 +107,7 @@ describe("Koa Logtail tests", () => {
.expect(404);
});

it("should log at 'error' level when status is 500", async done => {
it("should log at 'error' level when status is 500", async () => {
const [logtail, koa] = getServer();

// Mock out the sync method
Expand All @@ -130,9 +121,6 @@ describe("Koa Logtail tests", () => {
// Should be 'error' level
expect(logs[0].level).toBe(LogLevel.Error);

// Finish task
await done();

return logs;
});

Expand All @@ -141,7 +129,7 @@ describe("Koa Logtail tests", () => {
.expect(500);
});

it("should log at 'error' level when Koa middleware throws", async done => {
it("should log at 'error' level when Koa middleware throws", async () => {
const [logtail, koa] = getServer();

// Error to throw
Expand All @@ -163,9 +151,6 @@ describe("Koa Logtail tests", () => {
// Should be 'error' level
expect(logs[0].level).toBe(LogLevel.Error);

// Finish task
await done();

return logs;
});

Expand All @@ -174,7 +159,7 @@ describe("Koa Logtail tests", () => {
.expect(404);
});

it("should not log 'Info' logs when the level is 'Warn'", async done => {
it("should not log 'Info' logs when the level is 'Warn'", async () => {
const [logtail, koa] = getServer();

logtail.setLevel(LogLevel.Warn);
Expand All @@ -190,9 +175,6 @@ describe("Koa Logtail tests", () => {
// Should be 'warn' level
expect(logs[0].level).toBe(LogLevel.Warn);

// Finish task
await done();

return logs;
});

Expand Down
4 changes: 1 addition & 3 deletions packages/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("node tests", () => {
await logtail.log(message);
});

it("should pipe logs to a writable file stream", async done => {
it("should pipe logs to a writable file stream", async () => {
// Create a temporary file name
const temp = path.join(os.tmpdir(), `logtail_${Math.random()}`);

Expand Down Expand Up @@ -127,8 +127,6 @@ describe("node tests", () => {
for (let i = 0; i < messages.length; i++) {
expect(data[i].message).toEqual(messages[i]);
}

done();
});

writeStream.end();
Expand Down
Loading

0 comments on commit 1acc0aa

Please sign in to comment.