Skip to content

Commit

Permalink
Migrate unit tests in test/e2e folder to esm module syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefizso committed Oct 22, 2024
1 parent d9cbfef commit aa96d42
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions test/e2e/toCanvas.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'tap'
const { Canvas, createCanvas } = require('canvas')
const QRCode = require('lib')
const Helpers = require('test/helpers')
import { Canvas, createCanvas } from 'canvas'
import * as QRCode from '../../lib/index.js'
import * as Helpers from '../helpers.js'

test('toCanvas - no promise available', function (t) {
Helpers.removeNativePromise()
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/toDataURL.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from 'tap'
const QRCode = require('lib')
const QRCodeBrowser = require('lib/browser')
const { createCanvas } = require('canvas')
const Helpers = require('test/helpers')
import * as QRCode from '../../lib/index.js'
import * as QRCodeBrowser from '../../lib/browser.js'
import { createCanvas } from 'canvas'
import * as Helpers from '../helpers.js'

test('toDataURL - no promise available', function (t) {
Helpers.removeNativePromise()
Expand Down
25 changes: 15 additions & 10 deletions test/e2e/toFile.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { test } from 'tap'
const fs = require('fs')
const path = require('path')
const os = require('os')
const sinon = require('sinon')
const QRCode = require('lib')
const Helpers = require('test/helpers')
const StreamMock = require('test/mocks/writable-stream')
import fs from 'fs'
import path from 'path'
import os from 'os'
import sinon from 'sinon'
import * as QRCode from './../../lib/index.js'
import * as Helpers from './../helpers.js'
import StreamMock from './../mocks/writable-stream.js'

test('toFile - no promise available', function (t) {
Helpers.removeNativePromise()
Expand Down Expand Up @@ -56,7 +56,7 @@ test('toFile png', function (t) {
'qVO5LQqTxRrFGKNUqxRon/scYo1ijFGqVYoxRrlGKNUqxRijVKsUYp1ijFGqVYoxRrlGKN',
'UqxRijXKP0OHEepgrecVAAAAAElFTkSuQmCC'].join('')

t.plan(8)
t.plan(6)

QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
Expand Down Expand Up @@ -98,6 +98,12 @@ test('toFile png', function (t) {
'Should write correct content (promise)')
})
})
})

test('toFile png - error writing to stream', { todo: 'Mocking streams does not work in ESM modules.' }, function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage-failed.png')

t.plan(2)

const fsStub = sinon.stub(fs, 'createWriteStream')
fsStub.returns(new StreamMock().forceErrorOnWrite())
Expand All @@ -119,8 +125,7 @@ test('toFile png', function (t) {

test('toFile svg', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage.svg')
const expectedOutput = fs.readFileSync(
path.join(__dirname, '/svg.expected.out'), 'UTF-8')
const expectedOutput = fs.readFileSync(new URL('./svg.expected.out', import.meta.url), 'UTF-8')

t.plan(6)

Expand Down
6 changes: 3 additions & 3 deletions test/e2e/toFileStream.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'tap'
const sinon = require('sinon')
const QRCode = require('lib')
const StreamMock = require('../mocks/writable-stream')
import sinon from 'sinon'
import * as QRCode from './../../lib/index.js'
import StreamMock from './../mocks/writable-stream.js'

test('toFileStream png', function (t) {
t.throws(function () { QRCode.toFileStream('some text') },
Expand Down
15 changes: 7 additions & 8 deletions test/e2e/toString.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { test } from 'tap'
const fs = require('fs')
const path = require('path')
const QRCode = require('lib')
const browser = require('lib/browser')
const Helpers = require('test/helpers')
import fs from 'fs'
import * as QRCode from './../../lib/index.js'
import * as browser from './../../lib/browser.js'
import * as Helpers from './../helpers.js'

test('toString - no promise available', function (t) {
Helpers.removeNativePromise()
Expand Down Expand Up @@ -76,7 +75,7 @@ test('toString (browser)', function (t) {
})

test('toString svg', function (t) {
const file = path.join(__dirname, '/svgtag.expected.out')
const file = new URL('./svgtag.expected.out', import.meta.url)
t.plan(6)

QRCode.toString('http://www.google.com', {
Expand Down Expand Up @@ -121,7 +120,7 @@ test('toString svg', function (t) {
})

test('toString browser svg', function (t) {
const file = path.join(__dirname, '/svgtag.expected.out')
const file = new URL('./svgtag.expected.out', import.meta.url)

t.plan(3)

Expand Down Expand Up @@ -212,7 +211,7 @@ test('toString utf8', function (t) {
})

test('toString terminal', function (t) {
const expectedTerminal = fs.readFileSync(path.join(__dirname, '/terminal.expected.out')) + ''
const expectedTerminal = fs.readFileSync(new URL('./terminal.expected.out', import.meta.url)) + ''

t.plan(3)

Expand Down

0 comments on commit aa96d42

Please sign in to comment.