Skip to content

Commit

Permalink
chore: refine test style
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 16, 2020
1 parent 43438e9 commit 9b7a799
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,11 @@ describe('Koa Instrumentation - Router Tests', () => {
app.use((ctx, next) => tracer.withSpan(rootSpan, next));

const router = new KoaRouter();
router.get('/', ctx => {
ctx.body = 'list';
});
router.get('/post/new', ctx => {
ctx.body = 'add';
});
router.get('/post/:id', ctx => {
const id = ctx.params.id;
ctx.body = 'Post id: ' + id;
});

router.post('/post', async ctx => {
const post = ctx.body;
post.created_at = new Date();
ctx.redirect('/');
});

app.use(router.routes());

const server = http.createServer(app.callback());
Expand All @@ -110,7 +98,7 @@ describe('Koa Instrumentation - Router Tests', () => {
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 2);
const requestHandlerSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name.includes('/post/:id'));
.find(span => span.name.includes('router - /post/:id'));
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.COMPONENT],
Expand Down
15 changes: 7 additions & 8 deletions plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as http from 'http';
import { AddressInfo } from 'net';
import { plugin } from '../src';
import { AttributeNames, KoaLayerType, KoaComponentName } from '../src/types';
import { nextTick } from 'process';

const httpRequest = {
get: (options: http.ClientRequestArgs | string) => {
Expand Down Expand Up @@ -71,16 +72,16 @@ describe('Koa Instrumentation - Core Tests', () => {
context.disable();
});

const simpleResponse: koa.Middleware = (ctx, next) => {
const simpleResponse: koa.Middleware = async (ctx, next) => {
ctx.body = 'test';
return next();
await next();
};

const customMiddleware: koa.Middleware = (ctx, next) => {
const customMiddleware: koa.Middleware = async (ctx, next) => {
for (let i = 0; i < 1000000; i++) {
continue;
}
return next();
await next();
};

const asyncMiddleware: koa.Middleware = async (ctx, next) => {
Expand All @@ -98,10 +99,6 @@ describe('Koa Instrumentation - Core Tests', () => {
app.use(customMiddleware);
app.use(simpleResponse);

app.use((ctx, next) => {
ctx.body = 'anonymous';
return next();
});

const server = http.createServer(app.callback());
await new Promise(resolve => server.listen(0, resolve));
Expand All @@ -111,6 +108,7 @@ describe('Koa Instrumentation - Core Tests', () => {
await tracer.withSpan(rootSpan, async () => {
await httpRequest.get(`http://localhost:${port}`);
rootSpan.end();
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 5);

assert.notStrictEqual(
memoryExporter
Expand Down Expand Up @@ -173,6 +171,7 @@ describe('Koa Instrumentation - Core Tests', () => {
await tracer.withSpan(rootSpan, async () => {
await httpRequest.get(`http://localhost:${port}`);
rootSpan.end();
assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 3);

const requestHandlerSpan = memoryExporter
.getFinishedSpans()
Expand Down

0 comments on commit 9b7a799

Please sign in to comment.