Skip to content

Commit d6de80f

Browse files
authored
fix: revert supporting new.target attribute (#33)
1 parent 931f4fd commit d6de80f

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/spy.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ export function spy<A extends any[], R>(
5959
let type: 'ok' | 'error' = 'ok'
6060
if (fn.impl) {
6161
try {
62-
result = new.target
63-
? new (fn.impl as any)(...args)
64-
: fn.impl.apply(this, args)
62+
result = fn.impl.apply(this, args)
6563
type = 'ok'
6664
} catch (err: any) {
6765
result = err

test/class.test.ts

+41-28
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,53 @@ describe('class mock', () => {
2525
expect(spy2.callCount).toBe(1)
2626
})
2727

28-
test('throws error, if constructor is an arrow function', () => {
29-
const fnArrow = spy(() => {})
30-
expect(() => new fnArrow()).toThrowError()
28+
test('spy keeps instance', () => {
29+
const obj = {
30+
Test() {},
31+
}
32+
const fn = spyOn(obj, 'Test')
33+
const instance = new obj.Test()
34+
expect(fn.called).toBe(true)
35+
expect(instance).toBeInstanceOf(obj.Test)
3136
})
3237

33-
test('respects new.target in a function', () => {
34-
let target: unknown = null
35-
let args: unknown[] = []
36-
const fnScoped = spy(function (...fnArgs: unknown[]) {
37-
target = new.target
38-
args = fnArgs
38+
// FIXME: https://github.com/tinylibs/tinyspy/issues/29
39+
describe.todo('spying on constructor', () => {
40+
test('throws error, if constructor is an arrow function', () => {
41+
const fnArrow = spy(() => {})
42+
expect(() => new fnArrow()).toThrowError()
3943
})
4044

41-
new fnScoped('some', 'text', 1)
42-
expect(target).toBeTypeOf('function')
43-
expect(args).toEqual(['some', 'text', 1])
44-
expect(fnScoped.calls).toEqual([['some', 'text', 1]])
45-
})
45+
test('respects new.target in a function', () => {
46+
let target: unknown = null
47+
let args: unknown[] = []
48+
const fnScoped = spy(function (...fnArgs: unknown[]) {
49+
target = new.target
50+
args = fnArgs
51+
})
52+
53+
new fnScoped('some', 'text', 1)
54+
expect(target).toBeTypeOf('function')
55+
expect(args).toEqual(['some', 'text', 1])
56+
expect(fnScoped.calls).toEqual([['some', 'text', 1]])
57+
})
4658

47-
test('respects new.target in a class', () => {
48-
let target: unknown = null
49-
let args: unknown[] = []
50-
const fnScoped = spy(
51-
class {
52-
constructor(...fnArgs: unknown[]) {
53-
target = new.target
54-
args = fnArgs
59+
test('respects new.target in a class', () => {
60+
let target: unknown = null
61+
let args: unknown[] = []
62+
const fnScoped = spy(
63+
class {
64+
constructor(...fnArgs: unknown[]) {
65+
target = new.target
66+
args = fnArgs
67+
}
5568
}
56-
}
57-
)
69+
)
5870

59-
new fnScoped('some', 'text', 1)
60-
expect(target).toBeTypeOf('function')
61-
expect(args).toEqual(['some', 'text', 1])
62-
expect(fnScoped.calls).toEqual([['some', 'text', 1]])
71+
new fnScoped('some', 'text', 1)
72+
expect(target).toBeTypeOf('function')
73+
expect(args).toEqual(['some', 'text', 1])
74+
expect(fnScoped.calls).toEqual([['some', 'text', 1]])
75+
})
6376
})
6477
})

0 commit comments

Comments
 (0)