Discrepancy in jest regex and browser console test

Viewed 45
import { COMMENT_REGEX } from './regex'

describe('comment regex', () => {
  it('returns true for empty comment', () => {
    const comment = '//'
    expect(COMMENT_REGEX.test(comment)).toBe(true)
  })

  it('returns true if there is a comment', () => {
    const comment = '// This is a comment'
    expect(COMMENT_REGEX.test(comment)).toBe(true)
  })

  it('returns false if there is no comment', () => {
    const instr = 'D=M+1'
    expect(COMMENT_REGEX.test(instr)).toBe(false)
  })
})

regex.js

export const COMMENT_REGEX = /\/\/[^\n]*/g

I'm not sure why the test is failing, I tried in browser console both strings and it is working as expected,

/\/\/[^\n]*/g.test('//')  // true
/\/\/[^\n]*/g.test('// This is a comment') // true

why it is failing in jest?

expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

      13 |   it('returns true if there is a comment', () => {
      14 |     const comment = '// This is a comment'
    > 15 |     expect(COMMENT_REGEX.test(comment)).toBe(true)
         |                                          ^
      16 |   })
      17 |
      18 |   it('returns false if there is no comment', () => {

      at Object.it (src/abstractions/software/assembler/parser/utils/regex.test.js:15:42)
0 Answers
Related