Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: improve interchangeability with cjs (#1944)
- Loading branch information
1 parent
456079e
commit 4131a52
Showing
12 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"tinyspy": "^1.0.2", | ||
"vitest": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = [1, '2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { c: 'c' } | ||
exports.a = 'a' | ||
exports.b = 'b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Test { | ||
variable = 1 | ||
} | ||
|
||
module.exports = new Test() | ||
module.exports.Test = Test |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = 'string' | ||
exports.a = 'a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from 'tinyspy' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,63 @@ | ||
import { expect, it } from 'vitest' | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
import { a, b } from '../src/module-cjs' | ||
// @ts-expect-error is not typed | ||
import cjs, { a, b } from '../src/cjs/module-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import bareCjs, { a as bareA, b as bareB } from '../src/cjs/bare-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import primitiveCjs, { a as primitiveA } from '../src/cjs/primitive-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import * as primitiveAll from '../src/cjs/primitive-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import * as arrayCjs from '../src/cjs/array-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import * as classCjs from '../src/cjs/class-cjs' | ||
// @ts-expect-error is not typed with imports | ||
import * as internalEsm from '../src/esm/internal-esm.mjs' | ||
import c, { d } from '../src/module-esm' | ||
import * as timeout from '../src/timeout' | ||
|
||
it('doesn\'t when extending module', () => { | ||
expect(() => Object.assign(globalThis, timeout)).not.toThrow() | ||
}) | ||
|
||
it('should work when using cjs module', () => { | ||
it('should work when using module.exports cjs', () => { | ||
expect(cjs.a).toBe(1) | ||
expect(cjs.b).toBe(2) | ||
expect(a).toBe(1) | ||
expect(b).toBe(2) | ||
}) | ||
|
||
it('works with bare exports cjs', () => { | ||
expect(bareCjs.a).toBe('a') | ||
expect(bareCjs.b).toBe('b') | ||
expect(bareCjs.c).toBe('c') | ||
expect(bareA).toBe('a') | ||
expect(bareB).toBe('b') | ||
}) | ||
|
||
it('primitive cjs retains its logic', () => { | ||
expect(primitiveA).toBeUndefined() | ||
expect(primitiveCjs).toBe('string') | ||
expect(primitiveAll.default).toBe('string') | ||
expect(primitiveAll, 'doesn\'t put chars from "string" on exports').not.toHaveProperty('0') | ||
}) | ||
|
||
it('arrays-cjs', () => { | ||
expect(arrayCjs.default).toEqual([1, '2']) | ||
expect(arrayCjs).not.toHaveProperty('0') | ||
}) | ||
|
||
it('class-cjs', () => { | ||
expect(classCjs.default).toEqual({ variable: 1, Test: expect.any(Function) }) | ||
expect(classCjs.default).toBeInstanceOf(classCjs.Test) | ||
expect(classCjs).not.toHaveProperty('variable') | ||
}) | ||
|
||
it('should work when using esm module', () => { | ||
expect(c).toBe(1) | ||
expect(d).toBe(2) | ||
}) | ||
|
||
it('exports all from native ESM module', () => { | ||
expect(internalEsm).toHaveProperty('restoreAll') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters