mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
chore: migrate test cases from Chai to Jest
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import { expect, use } from 'chai'
|
||||
import { RenderError } from '../../../src/util/error'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
import * as path from 'path'
|
||||
import { mock, restore } from '../../stub/mockfs'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
use(chaiAsPromised)
|
||||
|
||||
let engine = new Liquid()
|
||||
const strictEngine = new Liquid({
|
||||
@@ -18,9 +14,10 @@ describe('error', function () {
|
||||
|
||||
describe('TokenizationError', function () {
|
||||
it('should throw TokenizationError when tag illegal', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% . a %}', {})).be.rejected
|
||||
expect(err.name).to.equal('TokenizationError')
|
||||
expect(err.message).to.contain('illegal tag syntax')
|
||||
await expect(engine.parseAndRender('{% . a %}', {})).rejects.toMatchObject({
|
||||
name: 'TokenizationError',
|
||||
message: expect.stringContaining('illegal tag syntax')
|
||||
})
|
||||
})
|
||||
it('should contain template content in err.message', async function () {
|
||||
const html = ['1st', '2nd', 'X{% . a %} Y', '4th']
|
||||
@@ -31,33 +28,41 @@ describe('error', function () {
|
||||
' 4| 4th',
|
||||
'TokenizationError'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
|
||||
expect(err.message).to.equal('illegal tag syntax, line:3, col:2')
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('TokenizationError')
|
||||
await expect(engine.parseAndRender(html.join('\n'))).rejects.toMatchObject({
|
||||
message: 'illegal tag syntax, line:3, col:2',
|
||||
stack: expect.stringContaining(message.join('\n')),
|
||||
name: 'TokenizationError'
|
||||
})
|
||||
})
|
||||
it('should contain the whole template content in err.token.input', async function () {
|
||||
const html = 'bar\nfoo{% . a %}\nfoo'
|
||||
const err = await expect(engine.parseAndRender(html)).be.rejected
|
||||
expect(err.token.input).to.equal(html)
|
||||
await expect(engine.parseAndRender(html)).rejects.toMatchObject({
|
||||
token: expect.objectContaining({
|
||||
input: html
|
||||
})
|
||||
})
|
||||
})
|
||||
it('should contain stack in err.stack', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected
|
||||
expect(err.message).to.contain('illegal tag syntax')
|
||||
expect(err.stack).to.contain('at Liquid.parse')
|
||||
await expect(engine.parseAndRender('{% . a %}')).rejects.toMatchObject({
|
||||
message: expect.stringContaining('illegal tag syntax'),
|
||||
stack: expect.stringContaining('at Liquid.parse')
|
||||
})
|
||||
})
|
||||
describe('captureStackTrace compatibility', function () {
|
||||
it('should be empty when captureStackTrace undefined', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected
|
||||
expect(err.stack).to.contain('illegal tag syntax')
|
||||
expect(err.stack).to.not.contain('at Object.parse')
|
||||
await expect(engine.parseAndRender('{% . a %}')).rejects.toMatchObject({
|
||||
stack: expect.stringContaining('illegal tag syntax')
|
||||
})
|
||||
await expect(engine.parseAndRender('{% . a %}')).rejects.toMatchObject({
|
||||
stack: expect.not.stringContaining('at Object.parse')
|
||||
})
|
||||
})
|
||||
})
|
||||
it('should throw error with [line, col] if tag unmatched', async function () {
|
||||
const err = await expect(engine.parseAndRender('1\n2\nfoo{% assign a = 4 }\n4')).be.rejected
|
||||
console.log(err.stack)
|
||||
expect(err.name).to.equal('TokenizationError')
|
||||
expect(err.message).to.equal('tag "{% assign a =..." not closed, line:3, col:4')
|
||||
await expect(engine.parseAndRender('1\n2\nfoo{% assign a = 4 }\n4')).rejects.toMatchObject({
|
||||
name: 'TokenizationError',
|
||||
message: 'tag "{% assign a =..." not closed, line:3, col:4'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -82,30 +87,34 @@ describe('error', function () {
|
||||
})
|
||||
it('should throw RenderError when tag throws', async function () {
|
||||
const src = '{%throwingTag%}'
|
||||
const err = await expect(engine.parseAndRender(src)).be.rejected
|
||||
expect(err.name).to.equal('RenderError')
|
||||
expect(err.message).to.contain('intended render error')
|
||||
await expect(engine.parseAndRender(src)).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: expect.stringContaining('intended render error')
|
||||
})
|
||||
})
|
||||
it('should throw RenderError when tag rejects', async function () {
|
||||
const src = '{%rejectingTag%}'
|
||||
const err = await expect(engine.parseAndRender(src)).be.rejected
|
||||
expect(err.name).to.equal('RenderError')
|
||||
expect(err.message).to.contain('intended render reject')
|
||||
await expect(engine.parseAndRender(src)).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: expect.stringContaining('intended render reject')
|
||||
})
|
||||
})
|
||||
it('should throw RenderError when filter throws', async function () {
|
||||
const src = '{{1|throwingFilter}}'
|
||||
const err = await expect(engine.parseAndRender(src)).be.rejected
|
||||
expect(err.name).to.equal('RenderError')
|
||||
expect(err.message).to.contain('throwed by filter')
|
||||
await expect(engine.parseAndRender(src)).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: expect.stringContaining('throwed by filter')
|
||||
})
|
||||
})
|
||||
it('should not throw when variable undefined by default', async function () {
|
||||
const html = await engine.parseAndRender('X{{a}}Y')
|
||||
return expect(html).to.equal('XY')
|
||||
return expect(html).toBe('XY')
|
||||
})
|
||||
it('should throw RenderError when variable not defined', async function () {
|
||||
const err = await expect(strictEngine.parseAndRender('{{a}}')).be.rejected
|
||||
expect(err).to.have.property('name', 'RenderError')
|
||||
expect(err.message).to.contain('undefined variable: a')
|
||||
await expect(strictEngine.parseAndRender('{{a}}')).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: expect.stringContaining('undefined variable: a')
|
||||
})
|
||||
})
|
||||
it('should contain template context in err.stack', async function () {
|
||||
const html = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
|
||||
@@ -118,10 +127,11 @@ describe('error', function () {
|
||||
' 7| 7th',
|
||||
'RenderError'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
|
||||
expect(err.message).to.equal('intended render error, line:4, col:2')
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('RenderError')
|
||||
await expect(engine.parseAndRender(html.join('\n'))).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: 'intended render error, line:4, col:2',
|
||||
stack: expect.stringContaining(message.join('\n'))
|
||||
})
|
||||
})
|
||||
it('should contain original error info for {% layout %}', async function () {
|
||||
mock({
|
||||
@@ -145,10 +155,11 @@ describe('error', function () {
|
||||
' 7| 7th',
|
||||
'RenderError'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html)).be.rejected
|
||||
expect(err.message).to.equal(`intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`)
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('RenderError')
|
||||
await expect(engine.parseAndRender(html)).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: `intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`,
|
||||
stack: expect.stringContaining(message.join('\n'))
|
||||
})
|
||||
})
|
||||
it('should contain original error info for {% include %}', async function () {
|
||||
const origin = ['1st', '2nd', '3rd', 'X{%throwingTag%} Y', '5th', '6th', '7th']
|
||||
@@ -165,15 +176,17 @@ describe('error', function () {
|
||||
' 7| 7th',
|
||||
'RenderError'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html)).be.rejected
|
||||
expect(err.message).to.equal(`intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`)
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('RenderError')
|
||||
await expect(engine.parseAndRender(html)).rejects.toMatchObject({
|
||||
name: 'RenderError',
|
||||
message: `intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`,
|
||||
stack: expect.stringContaining(message.join('\n'))
|
||||
})
|
||||
})
|
||||
it('should contain stack in err.stack', async function () {
|
||||
const err = await expect(engine.parseAndRender('{%rejectingTag%}')).be.rejected
|
||||
expect(err.message).to.contain('intended render reject')
|
||||
expect(err.stack).to.match(/at .*:\d+:\d+/)
|
||||
await expect(engine.parseAndRender('{%rejectingTag%}')).rejects.toMatchObject({
|
||||
message: expect.stringContaining('intended render reject'),
|
||||
stack: expect.stringMatching(/at .*:\d+:\d+/)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -188,30 +201,35 @@ describe('error', function () {
|
||||
})
|
||||
})
|
||||
it('should throw ParseError when filter not defined', async function () {
|
||||
const err = await expect(strictEngine.parseAndRender('{{1 | a}}')).be.rejected
|
||||
expect(err).to.have.property('name', 'ParseError')
|
||||
expect(err.message).to.contain('undefined filter: a')
|
||||
await expect(strictEngine.parseAndRender('{{1 | a}}')).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: expect.stringContaining('undefined filter: a')
|
||||
})
|
||||
})
|
||||
it('should throw ParseError when tag not closed', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% if %}')).be.rejected
|
||||
expect(err.name).to.equal('ParseError')
|
||||
expect(err.message).to.contain('tag {% if %} not closed')
|
||||
await expect(engine.parseAndRender('{% if %}')).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: expect.stringContaining('tag {% if %} not closed')
|
||||
})
|
||||
})
|
||||
it('should throw ParseError when tag parse throws', async function () {
|
||||
const err = await expect(engine.parseAndRender('{%throwsOnParse%}')).be.rejected
|
||||
expect(err.name).to.equal('ParseError')
|
||||
expect(err.message).to.contain('intended parse error')
|
||||
await expect(engine.parseAndRender('{%throwsOnParse%}')).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: expect.stringContaining('intended parse error')
|
||||
})
|
||||
})
|
||||
it('should throw ParseError when tag not found', async function () {
|
||||
const src = '{%if true%}\naaa{%endif%}\n{% -a %}\n3'
|
||||
const err = await expect(engine.parseAndRender(src)).be.rejected
|
||||
expect(err.name).to.equal('ParseError')
|
||||
expect(err.message).to.contain('tag "-a" not found')
|
||||
await expect(engine.parseAndRender(src)).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: expect.stringContaining('tag "-a" not found')
|
||||
})
|
||||
})
|
||||
it('should throw ParseError when tag not exist', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% a %}')).be.rejected
|
||||
expect(err.name).to.equal('ParseError')
|
||||
expect(err.message).to.contain('tag "a" not found')
|
||||
await expect(engine.parseAndRender('{% a %}')).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: expect.stringContaining('tag "a" not found')
|
||||
})
|
||||
})
|
||||
|
||||
it('should contain template context in err.stack', async function () {
|
||||
@@ -225,10 +243,11 @@ describe('error', function () {
|
||||
' 7| 7th',
|
||||
'ParseError: tag "a" not found'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
|
||||
expect(err.message).to.equal('tag "a" not found, line:4, col:2')
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('ParseError')
|
||||
await expect(engine.parseAndRender(html.join('\n'))).rejects.toMatchObject({
|
||||
name: 'ParseError',
|
||||
message: 'tag "a" not found, line:4, col:2',
|
||||
stack: expect.stringContaining(message.join('\n'))
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle err.message when context not enough', async function () {
|
||||
@@ -240,15 +259,19 @@ describe('error', function () {
|
||||
' 4| 4th',
|
||||
'ParseError: tag "a" not found'
|
||||
]
|
||||
const err = await expect(engine.parseAndRender(html.join('\n'))).be.rejected
|
||||
expect(err.message).to.equal('tag "a" not found, line:2, col:2')
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
await expect(engine.parseAndRender(html.join('\n'))).rejects.toMatchObject({
|
||||
message: 'tag "a" not found, line:2, col:2',
|
||||
stack: expect.stringContaining(message.join('\n'))
|
||||
})
|
||||
})
|
||||
|
||||
it('should contain stack in err.stack', async function () {
|
||||
const err = await expect(engine.parseAndRender('{% -a %}')).be.rejected
|
||||
expect(err.stack).to.contain('ParseError: tag "-a" not found')
|
||||
expect(err.stack).to.match(/at .*:\d+:\d+\)/)
|
||||
await expect(engine.parseAndRender('{% -a %}')).rejects.toMatchObject({
|
||||
stack: expect.stringContaining('ParseError: tag "-a" not found')
|
||||
})
|
||||
await expect(engine.parseAndRender('{% -a %}')).rejects.toMatchObject({
|
||||
stack: expect.stringMatching(/at .*:\d+:\d+\)/)
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('sync support', function () {
|
||||
@@ -265,8 +288,8 @@ describe('error', function () {
|
||||
})
|
||||
it('should throw RenderError when tag throws', function () {
|
||||
const src = '{%throwingTag%}'
|
||||
expect(() => engine.parseAndRenderSync(src))
|
||||
.to.throw(RenderError, /intended render error/)
|
||||
expect(() => engine.parseAndRenderSync(src)).toThrow(RenderError)
|
||||
expect(() => engine.parseAndRenderSync(src)).toThrow(/intended render error/)
|
||||
})
|
||||
it('should contain original error info for {% include %}', function () {
|
||||
mock({
|
||||
@@ -286,9 +309,9 @@ describe('error', function () {
|
||||
engine.parseAndRenderSync(html)
|
||||
throw new Error('expected throw')
|
||||
} catch (err) {
|
||||
expect(err.message).to.equal(`intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`)
|
||||
expect(err.stack).to.contain(message.join('\n'))
|
||||
expect(err.name).to.equal('RenderError')
|
||||
expect(err).toHaveProperty('name', 'RenderError')
|
||||
expect(err).toHaveProperty('message', `intended render error, file:${path.resolve('/throwing-tag.html')}, line:4, col:2`)
|
||||
expect(err).toHaveProperty('stack', expect.stringContaining(message.join('\n')))
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user