Skip to content

Commit

Permalink
add a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khanayan123 committed Dec 18, 2023
1 parent 8b45998 commit 61876ee
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 6 deletions.
Empty file added test/fixtures/cyclical-a.mjs
Empty file.
7 changes: 7 additions & 0 deletions test/fixtures/cyclical-b.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { testB } from './b.mjs';

export function testA() {
console.log("testA");
}

testB();
8 changes: 8 additions & 0 deletions test/fixtures/env.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let env = {FOO: 'baz'};

function setEnv(newEnv) {
console.log('setting env, env.FOO is', newEnv.FOO);
env = newEnv;
}

export { setEnv, env };
19 changes: 19 additions & 0 deletions test/fixtures/export-types/declarations.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const o = { name5: 1, name6: 1 };
const array = [1, 1]

// Exporting declarations
export let name1 = 1, name2 = 1/*, … */; // also var
export const name3 = 1, name4 = 1/*, … */; // also var, let
export function functionName() { return 1 }
export class ClassName { getFoo() { return 1 } }
export function* generatorFunctionName() { return 1 }
export const { name5, name6: bar } = o;
export const [ name7, name8 ] = array;
export async function asyncFunctionName() { return 1 }
export async function* asyncGeneratorFunctionName() { yield 1 }
export const arrowFunction = () => {
return 1;
}
export const asyncArrowFunction = async () => {
return 1;
}
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-class-anon.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class { getFoo() { return 1 } }
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-class.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class ClassName { getFoo() { return 1 } }
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-expression-array.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [1]
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-expression-num.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-expression-string.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'dog'
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-function-anon.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function () { return 1 }
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-function.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function functionName() { return 1 }
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-generator-anon.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function* () { return 1 }
1 change: 1 addition & 0 deletions test/fixtures/export-types/default-generator.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function* generatorFunctionName() { return 1 }
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions test/fixtures/export-types/list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Export list
const name1 = 1
const name2 = 1
const name5 = 1
const variable1 = 1
const variable2 = 1
const variable3 = 1
const name6 = 1
export { name1, name2 };
export { variable1 as name3, variable2 as name4, /* …, */ name5 };
export { variable3 as "name" };
export { name6 as default /*, … */ };
File renamed without changes.
75 changes: 69 additions & 6 deletions test/hook/default-export.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,84 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import Hook from '../../index.js'
import defaultImportExport from '../fixtures/import-default-export.mjs'
import varDefaultExport from '../fixtures/variable-default-export.mjs'
import a from '../fixtures/export-types/default-expression-array.mjs'
import n from '../fixtures/export-types/default-expression-num.mjs'
import s from '../fixtures/export-types/default-expression-string.mjs'
import fn from '../fixtures/export-types/default-function.mjs'
import cn from '../fixtures/export-types/default-class.mjs'
import gfn from '../fixtures/export-types/default-generator.mjs'
import afn from '../fixtures/export-types/default-function-anon.mjs'
import acn from '../fixtures/export-types/default-class-anon.mjs'
import agfn from '../fixtures/export-types/default-generator-anon.mjs'
import defaultImportExport from '../fixtures/export-types/import-default-export.mjs'
import varDefaultExport from '../fixtures/export-types/variable-default-export.mjs'
import { strictEqual } from 'assert'

Hook((exports, name) => {
if (name.match(/import-default-export\.m?js/)) {
if (name.match(/default-expression-array\.m?js/)) {
exports.default[0] += 1
} else if (name.match(/default-expression-num\.m?js/)) {
exports.default += 1
}
else if (name.match(/default-expression-string\.m?js/)) {
exports.default += 'dawg'
}
else if (name.match(/default-function\.m?js/)) {
const orig = exports.default
exports.default = function () {
return orig() + 1
}
} else if (name.match(/variable-default-export\.m?js/)) {
}
else if (name.match(/default-class\.m?js/)) {
exports.default.prototype.getFoo = function () {
return 2
}
}
else if (name.match(/default-generator\.m?js/)) {
const orig2 = exports.default
exports.default = function* () {
return orig2().next().value + 1
}
}
else if (name.match(/default-function-anon\.m?js/)) {
const orig = exports.default
exports.default = function () {
return orig() + 1
}
}
else if (name.match(/default-class-anon\.m?js/)) {
exports.default.prototype.getFoo = function () {
return 2
}
}
else if (name.match(/default-generator-anon\.m?js/)) {
const orig2 = exports.default
exports.default = function* () {
return orig2().next().value + 1
}
} else if (name.match(/import-default-export\.m?js/)) {
const orig3 = exports.default
exports.default = function () {
return orig3() + 1
}
} else if (name.match(/variable-default-export\.m?js/)) {
const orig4 = exports.default
exports.default = function () {
return orig2() + 1
return orig4() + 1
}
}
})

strictEqual(defaultImportExport(), 2)
strictEqual(varDefaultExport(), 2)
strictEqual(varDefaultExport(), 2)
strictEqual(a[0], 2)
strictEqual(fn(), 2)
strictEqual(new cn().getFoo(), 2)
strictEqual(gfn().next().value, 2)
strictEqual(afn(), 2)
strictEqual(new acn().getFoo(), 2)
strictEqual(agfn().next().value, 2)
strictEqual(n, 2)
strictEqual(s, 'dogdawg')
32 changes: 32 additions & 0 deletions test/other/assert-cyclical-dependency-failure.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { spawn } from 'child_process'
import { strictEqual } from 'assert'

const nodeProcess = spawn('node', [
'--loader',
'./hook.mjs',
'./test/fixtures/a.mjs'
])

// expected output is 'testB\ntestA' but the actual output of this test is '' because
// the hook fails when running against files with cylical dependencies
const expectedOutput = 'testB\ntestA'
const actualOutput = ''
let stdout = ''
let stderr = ''

nodeProcess.stdout.on('data', (data) => {
stdout += data.toString()
});

nodeProcess.stderr.on('data', (data) => {
stderr += data.toString()
});

nodeProcess.on('close', (code) => {
strictEqual(stderr, '', 'There should be no errors on stderr')
strictEqual(stdout.trim(), actualOutput, 'The stdout should match the expected output')
});

0 comments on commit 61876ee

Please sign in to comment.