jessevanassen Github contribution chart
jessevanassen Github Stats
jessevanassen Most Used Languages

Activity

07 Mar 2023

Jessevanassen

fix(types): Use any as default value for TArgs in vi.fn()

import { vi, type Mocked } from 'vitest';

type Subject = {
	doSomething(x: string): boolean;
};

const mockedSubject: Mocked<Subject> = {
	doSomething: vi.fn().mockReturnValue(true),
}; 

This snippet results in the following error when type checking:

error TS2322: Type 'Mock<any[], any>' is not assignable to type 'MockInstance<[x: string], boolean> & ((x: string) => boolean)'.
  Type 'Mock<any[], any>' is not assignable to type 'MockInstance<[x: string], boolean>'.
    The types of 'mockName(...).mock' are incompatible between these types.
      Type 'MockContext<any[], any>' is not assignable to type 'MockContext<[x: string], boolean>'.
        Type 'any[]' is not assignable to type '[x: string]'.
          Target requires 1 element(s) but source may have fewer.

10  doSomething: vi.fn().mockReturnValue(true),
    ~~~~~~~~~~~

  src/my.test.ts:4:2
    4  doSomething(x: string): boolean;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from property 'doSomething' which is declared here on type 'MockedSubject' 

This is because MockedSubject.doSomething knows that it requires exactly one parameter, but any[] (which is the default for TArgs) can have fewer.

The equivalent snippet in Jest, when using jest.Mocked<Subject> and jest.fn() doesn't result in an error, because in Jest the fn() without arguments returns a Mock, which has it's parameters initialized to any by default. This pull request makes the types of vi.fn() consistent with the types of jest.fn(), by setting the default value of TArgs to any.

Forked On 07 Mar 2023 at 05:58:29

Jessevanassen

I tried that with the example above, but when you pass in an empty function, the parameters will be inferred as `[]`, regardless if you initialize `TArgs` with `any[]` or `any` in the type definitions (or leave it out completely, [which is what `@types/jest` is doing](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/af319cfd1b58be68166cfb117bed37f1e4d45baa/types/jest/index.d.ts#L254)). This behavior is actually consistent between Vitest and Jest. **vitest.test.ts** (with `TArgs` set to `any`): ```ts import { vi, type Mocked } from 'vitest'; type Subject = { doSomething(x: string): boolean; }; const mockedSubject: Mocked<Subject> = { doSomething: vi.fn().mockReturnValue(true), }; const mockedSubjectWithoutArgs: Mocked<Subject> = { doSomething: vi.fn(() => true), }; /* src/vitest.test.ts:12:2 - error TS2322: Type 'Mock<[], boolean>' is not assignable to type 'MockInstance<[x: string], boolean> & ((x: string) => boolean)'. Type 'Mock<[], boolean>' is not assignable to type 'MockInstance<[x: string], boolean>'. The types of 'mockName(...).mock' are incompatible between these types. Type 'MockContext<[], boolean>' is not assignable to type 'MockContext<[x: string], boolean>'. Type '[]' is not assignable to type '[x: string]'. Source has 0 element(s) but target requires 1. 12 doSomething: vi.fn(() => true), ~~~~~~~~~~~ src/vitest.test.ts:4:2 4 doSomething(x: string): boolean; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The expected type comes from property 'doSomething' which is declared here on type 'Mocked<Subject>' */ const mockedSubjectWithCorrectArgs: Mocked<Subject> = { doSomething: vi.fn((x) => true), }; const mockedSubjectWithTooManyArgs: Mocked<Subject> = { doSomething: vi.fn((x, y) => true), }; /* src/vitest.test.ts:20:2 - error TS2322: Type 'Mock<[x: string, y: TArgs[1]], boolean>' is not assignable to type 'MockInstance<[x: string], boolean> & ((x: string) => boolean)'. Type 'Mock<[x: string, y: TArgs[1]], boolean>' is not assignable to type 'MockInstance<[x: string], boolean>'. The types of 'mockName(...).mock' are incompatible between these types. Type 'MockContext<[x: string, y: TArgs[1]], boolean>' is not assignable to type 'MockContext<[x: string], boolean>'. Type '[x: string, y: TArgs[1]]' is not assignable to type '[x: string]'. Source has 2 element(s) but target allows only 1. 20 doSomething: vi.fn((x, y) => true), ~~~~~~~~~~~ src/vitest.test.ts:4:2 4 doSomething(x: string): boolean; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The expected type comes from property 'doSomething' which is declared here on type 'Mocked<Subject>' */ ``` **jest.test.ts**: ```ts type Subject = { doSomething(x: string): boolean; }; const mockedSubject: jest.Mocked<Subject> = { doSomething: jest.fn().mockReturnValue(true), }; const mockedSubjectWithoutArgs: jest.Mocked<Subject> = { doSomething: jest.fn(() => true), }; /* src/jest.test.ts:10:2 - error TS2322: Type 'Mock<boolean, [], unknown>' is not assignable to type 'MockInstance<boolean, [x: string], unknown> & ((x: string) => boolean)'. Type 'Mock<boolean, [], unknown>' is not assignable to type 'MockInstance<boolean, [x: string], unknown>'. Types of property 'mock' are incompatible. Type 'MockContext<boolean, [], unknown>' is not assignable to type 'MockContext<boolean, [x: string], unknown>'. Type '[]' is not assignable to type '[x: string]'. Source has 0 element(s) but target requires 1. 10 doSomething: jest.fn(() => true), ~~~~~~~~~~~ src/jest.test.ts:2:2 2 doSomething(x: string): boolean; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The expected type comes from property 'doSomething' which is declared here on type 'Mocked<Subject>' */ const mockedSubjectWithCorrectArgs: jest.Mocked<Subject> = { doSomething: jest.fn((x) => true), }; const mockedSubjectWithTooManyArgs: jest.Mocked<Subject> = { doSomething: jest.fn((x, y) => true), }; /* src/jest.test.ts:18:2 - error TS2322: Type 'Mock<boolean, [x: string, y: Y[1]], unknown>' is not assignable to type 'MockInstance<boolean, [x: string], unknown> & ((x: string) => boolean)'. Type 'Mock<boolean, [x: string, y: Y[1]], unknown>' is not assignable to type 'MockInstance<boolean, [x: string], unknown>'. Types of property 'mock' are incompatible. Type 'MockContext<boolean, [x: string, y: Y[1]], unknown>' is not assignable to type 'MockContext<boolean, [x: string], unknown>'. Type '[x: string, y: Y[1]]' is not assignable to type '[x: string]'. Source has 2 element(s) but target allows only 1. 18 doSomething: jest.fn((x, y) => true), ~~~~~~~~~~~ src/jest.test.ts:2:2 2 doSomething(x: string): boolean; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The expected type comes from property 'doSomething' which is declared here on type 'Mocked<Subject>' */ ```
On 07 Mar 2023 at 05:58:29

Jessevanassen

fix(types): Use any as default value for TArgs in vi.fn()

import { vi, type Mocked } from 'vitest';

type Subject = {
	doSomething(x: string): boolean;
};

const mockedSubject: Mocked<Subject> = {
	doSomething: vi.fn().mockReturnValue(true),
}; 

This snippet results in the following error when type checking:

error TS2322: Type 'Mock<any[], any>' is not assignable to type 'MockInstance<[x: string], boolean> & ((x: string) => boolean)'.
  Type 'Mock<any[], any>' is not assignable to type 'MockInstance<[x: string], boolean>'.
    The types of 'mockName(...).mock' are incompatible between these types.
      Type 'MockContext<any[], any>' is not assignable to type 'MockContext<[x: string], boolean>'.
        Type 'any[]' is not assignable to type '[x: string]'.
          Target requires 1 element(s) but source may have fewer.

10  doSomething: vi.fn().mockReturnValue(true),
    ~~~~~~~~~~~

  src/my.test.ts:4:2
    4  doSomething(x: string): boolean;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from property 'doSomething' which is declared here on type 'MockedSubject' 

This is because MockedSubject.doSomething knows that it requires exactly one parameter, but any[] (which is the default for TArgs) can have fewer.

The equivalent snippet in Jest, when using jest.Mocked<Subject> and jest.fn() doesn't result in an error, because in Jest the fn() without arguments returns a Mock, which has it's parameters initialized to any by default. This pull request makes the types of vi.fn() consistent with the types of jest.fn(), by setting the default value of TArgs to any.

Merged On 07 Mar 2023 at 05:58:30

Jessevanassen

Commented On 07 Mar 2023 at 05:58:30

Jessevanassen

fix(types): Use any as default value for TArgs in vi.fn()

Created On 01 Mar 2023 at 06:35:20

Jessevanassen

fix(types): Use any as default value for TArgs in vi.fn()

Pushed On 01 Mar 2023 at 06:29:59
Create Branch
Jessevanassen In jessevanassen/vitest Create Branchtargs-any-for-vi.fn

Jessevanassen

A Vite-native test framework. It's fast!

On 01 Mar 2023 at 05:35:21

Jessevanassen

A Vite-native test framework. It's fast!

Forked On 01 Mar 2023 at 05:33:54

Jessevanassen

Description not entered by the user.

On 24 Feb 2023 at 02:54:59

Jessevanassen

Description not entered by the user.

On 24 Feb 2023 at 02:54:35

Jessevanassen

How to fix `port` `undefined`

Do not use latest commit https://github.com/microsoft/vscode-js-debug . use v1.74.1.

Error

[dap-js] JS Debugger stderr:
[dap-js] JS Debugger stderr: /path-to/vscode-js-debug/src/vsDebugServer.ts:93                                                                                                                 
    console.log((result.server.address() as net.AddressInfo).port.toString());                                                                                                                                                                
                                                                 ^                                                                                                                                                                            
TypeError: Cannot read properties of undefined (reading 'toString')                                                                                                                                                                           
    at VsDebugServer.launchRoot (/path-to/vscode-js-debug/src/vsDebugServer.ts:93:66)                                                                                                         
    at processTicksAndRejections (node:internal/process/task_queues:95:5)     
[dap-js] JS Debugger exited with code 1! 

Manually

git clone https://github.com/microsoft/vscode-js-debug
cd vscode-js-debug
git pull
git fetch --all --tags
git checkout tags/v1.74.1
npm install --legacy-peer-deps
npm run compile 

Forked On 31 Jan 2023 at 10:24:05

Jessevanassen

It should be fixed now and working with the main branch of vscode-js-debug again.

More info: https://github.com/microsoft/vscode-js-debug/issues/1502

Commented On 31 Jan 2023 at 10:24:05

Jessevanassen

TpeError: Cannot read properties of undefined (reading 'toString')

Describe the bug When starting the debugger node vsDebugServer.js I get the following error.

[dap-js] JS Debugger stderr:
[dap-js] JS Debugger stderr: ~/.local/share/nvim/site/pack/packer/opt/vscode-js-debug/src/vsDebugServer.ts:93
    console.log((result.server.address() as net.AddressInfo).port.toString());
                                                                 ^
TypeError: Cannot read properties of undefined (reading 'toString')
    at VsDebugServer.launchRoot (~/.local/share/nvim/site/pack/packer/opt/vscode-js-debug/src/vsDebugServer.ts:93:66)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

[dap-js] JS Debugger exited with code 1! 

To Reproduce

  1. Installed latest and uzzipped the lates build, 1.74.1
  2. ran npm install and npm run compile
  3. tsc --version Version 4.9.3

node -v v18.12.1

Forked On 30 Jan 2023 at 08:06:38

Jessevanassen

nvim-dap-vscode-js, which is where the above reports are coming from, isn't calling the script with any arguments.

It's starting the script here, getting the command and args from get_spawn_cmd. The cmd is the absolute path to Node, and the only argument is the path to vsDebugServer.js, so no additional arguments are passed.

Commented On 30 Jan 2023 at 08:06:38
Issue Comment

Jessevanassen

feat(jest-core): Add newline after Json output

Summary

This pull request adds newline characters at the end of Json objects produced with jest --json.

This makes it trivial to separate multiple test runs when running jest --json --watch. With the current behavior, all result objects will be concatenated without separators, requiring a streaming or custom parser to separate the test runs.

It also makes the output conform to the Unix file specification, which dicates that lines in a text file should end with an \n.

Fixes #13816

Test plan

All existing tests still pass.

Forked On 25 Jan 2023 at 04:49:36

Jessevanassen

I've amended the existing tests for the Json reporter to check for the final \n, for both the variant that writes to a file and to stdout.

I also needed to add an option to runJest to preserve the final newline in stdout / stderr. The tests use Execa to spawn Jest, which by default strips the final newline. Not great if you explicitly want to test if the final new line is present :smile:. I made it configurable so that the trailing new line can be preserved, but by default will be stripped to keep the original behavior.

Commented On 25 Jan 2023 at 04:49:36

Jessevanassen

Amend tests to check for trailing newline

Pushed On 25 Jan 2023 at 04:38:53

Jessevanassen

Update changelog

Pushed On 25 Jan 2023 at 03:28:19
Issue Comment

Jessevanassen

feat(jest-core): Add newline after Json output

Summary

This pull request adds newline characters at the end of Json objects produced with jest --json.

This makes it trivial to separate multiple test runs when running jest --json --watch. With the current behavior, all result objects will be concatenated without separators, requiring a streaming or custom parser to separate the test runs.

It also makes the output conform to the Unix file specification, which dicates that lines in a text file should end with an \n.

Fixes #13816

Test plan

All existing tests still pass.

Forked On 25 Jan 2023 at 03:19:15

Jessevanassen

can you add an integration test verifying the newline is present?

On it! 👍

Commented On 25 Jan 2023 at 03:19:15

Jessevanassen

Update changelog

Pushed On 25 Jan 2023 at 03:17:17

Jessevanassen

feat(jest-core): Add newline after Json output

Created On 25 Jan 2023 at 03:14:54
Create Branch
Jessevanassen In jessevanassen/jest Create Branchadd-newline-after-json-output

Jessevanassen

Delightful JavaScript Testing.

On 25 Jan 2023 at 01:36:06

Jessevanassen

Delightful JavaScript Testing.

Forked On 25 Jan 2023 at 01:27:58

Jessevanassen

Day 02: Add solutions

Pushed On 03 Jan 2023 at 08:11:45
Create Branch
Jessevanassen In jessevanassen/Advent-of-Code Create Branch2017/rust/main

Jessevanassen

My submissions for Advent of Code

On 03 Jan 2023 at 07:46:08

Jessevanassen

Day 16: Add solutions

Pushed On 02 Jan 2023 at 08:49:48

Jessevanassen

Day 19: Add solution for part 1, and pretty-printer for part 2

Pushed On 02 Jan 2023 at 08:49:48

Jessevanassen

Day 14: Add solutions

Pushed On 01 Jan 2023 at 08:32:50

Jessevanassen

Day 10: Add solutions

Pushed On 31 Dec 2022 at 07:29:08

Jessevanassen

Day 14: Add solutions

Pushed On 31 Dec 2022 at 07:29:08

Jessevanassen

Day 05: Add solutions

Pushed On 30 Dec 2022 at 09:41:03

Jessevanassen

Day 07: Add solutions

Pushed On 30 Dec 2022 at 09:41:03