Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: offset is longer than source length! is thrown when I try to update inline snapshot #626

Closed
6 tasks done
capaj opened this issue Jan 25, 2022 · 5 comments · Fixed by #3887
Closed
6 tasks done

Comments

@capaj
Copy link

capaj commented Jan 25, 2022

Describe the bug

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error: offset is longer than source length! offset 947 > length 278
    at Object.numberToPos (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/source-map-f7a4da05.js:3390:11)
    at prepareSnapString (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:700:44)
    at replaceObjectSnap (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:696:59)
    at replaceInlineSnap (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:715:12)
    at /home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:679:7
    at async Promise.all (index 0)
    at async saveInlineSnapshots (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:671:3)
    at async SnapshotState.save (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:839:9)
    at async packSnapshotState (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:1007:18)
    at async SnapshotClient.saveSnap (/home/capaj/work-repos/mindmed/node-comparison/node_modules/vitest/dist/entry.js:986:20)

Reproduction

I have this spec file

import { beforeAll, describe, expect, it } from 'vitest'
import { request } from 'undici'
import prismaClient from '../prisma/prismaClient'

describe('suite', () => {
  beforeAll(async () => {
    await prismaClient.questionnaire.deleteMany()
    await (
      await import('../server')
    ).listenPromise
  })

  describe('questionnares', () => {
    it('should GET', async () => {
      const res = await request('http://localhost:9090/questionnaires')

      expect(await res.body.json()).toMatchInlineSnapshot('[]')
    })

    it('should POST', async () => {
      const res = await request('http://localhost:9090/questionnaires', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          accept: 'application/json'
        },
        body: JSON.stringify({
          name: 'test',
          description: 'test',
          questionnaire: []
        })
      })

      expect(await res.body.json()).toMatchInlineSnapshot(
        {
          id: expect.any(Number)
        },
        `
          Object {
            "description": "test",
            "id": Any<Number>,
            "name": "test",
            "questionnaire": [],
          }
        `
      )
    })
  })
})

when I run the tests, I get this:

AssertionError: Snapshot `suite > questionnares > should POST 1` mismatched
 ❯ api/apiRoutes.spec.ts:34:36
     32|       })
     33| 
     34|       expect(await res.body.json()).toMatchInlineSnapshot(
       |                                    ^
     35|         {
     36|           id: expect.any(Number)

  - Expected  - 1
  + Received  + 1
  
  - Object {
  + {
      "description": "test",
      "id": Any<Number>,
      "name": "test",
      "questionnaire": [],

then I just hit u on the keyboard, error is thrown.

System Info

System:
    OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 7.60 GB / 62.75 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.yarn/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Browsers:
    Brave Browser: 97.1.34.80
    Chrome: 97.0.4692.99
    Firefox: 96.0
  npmPackages:
    vitest: ^0.2.1 => 0.2.1

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

This is really hard to test with this reproduction, since I don't have other files.

Does this error still happen to you? If so, can you make a reproduction on Stackblitz?

@sheremet-va
Copy link
Member

Closed because of no activity/reproduction.

@capaj
Copy link
Author

capaj commented Jan 3, 2023

I have a minimal repro here: https://stackblitz.com/edit/vitest-dev-vitest-hopf5m?file=test/suite.test.ts

image

when it runs, just hit u key to trigger snapshot update and you will see the bug.

@capaj
Copy link
Author

capaj commented Jan 3, 2023

@sheremet-va can you please reopen?

@velly
Copy link

velly commented Jun 12, 2023

I met this error because I made a mistake in types.ts;

@github-actions github-actions bot locked and limited conversation to collaborators Aug 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants