testdouble / testdouble/testdouble.js

`td.imitate` produces `undefined` for object keys of type `Accessor`

Open
#526 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.4k
Forks
140
Avg merge
22h
Merged PRs (30d)
1

Description

Description

I'm working on a Next.js app that uses testdouble.js for mocking dependencies in tests. Our test environment uses Jest and runs under Babel. This issue revealed itself when attempting to alter our Jest config to use the Next.js Compiler (SWC) as described here.

The following code snippet run under Babel results in a passing test.

const myDependency = td.replace("../lib");

td.when(myDependency.doTheThing()).thenReturn(123);

expect(myFunction()).toBe("some result");

The same test run under SWC fails with the error

TypeError: myDependency.doTheThing() is not a function
Issue

Objects with Accessor properties result in undefined when run through td.imitate.

For example, given:

class Foo {
  get bar() {
    return 0;
  }

  baz() {
    return 1;
  }
}

The line:

const instance = new Foo();

produces an object with the type

{ bar: Accessor, baz: Function }

Running the class instance through td.imitate

const imitation = td.imitate(instance);

results in an object with the type

{ bar: undefined, baz: Function }
Environment
  • node -v output:
  • npm -v (or yarn --version) output:
  • npm ls testdouble (or yarn list testdouble) version:
Failing Test
  • Fork the repo
  • Add a failing test (probably to the `/regression/src' directory)
  • Submit a pull request for the failing test or link to your branch here
Example Repo
  • Create a minimal repository that reproduces the issue
  • Make sure that a fresh clone can run only npm it and observe the issue
  • Link to that repo here
Runkit Notebook
  • Create a Runkit notebook
  • Invoke var td = require('testdouble') at the top
  • Verify the behavior your issue is concerned with by clicking "Run"
  • Link to the Runkit here

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the td.imitate entry point and reproduce the class instance case under the Jest/SWC setup described in the issue. Add a regression test in the regression/src directory, using the Accessor and Function properties shown; done means the imitation no longer produces undefined for the Accessor property while preserving the method.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.