FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-jsonfile/test/read-file-sync.test.js at master · GraphicDesignJH/node-jsonfile · GitHub
GraphicDesignJH
/
node-jsonfile
Public
forked from
jprichardson/node-jsonfile
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node-jsonfile
/
test
/
read-file-sync.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
149 lines (125 loc) · 3.94 KB
Breadcrumbs
node-jsonfile
/
test
/
read-file-sync.test.js
Copy path
File metadata and controls
149 lines (125 loc) · 3.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const
assert
=
require
(
'assert'
)
const
fs
=
require
(
'fs'
)
const
os
=
require
(
'os'
)
const
path
=
require
(
'path'
)
const
rimraf
=
require
(
'rimraf'
)
const
jf
=
require
(
'../'
)
/* global describe it beforeEach afterEach */
describe
(
'+ readFileSync()'
,
(
)
=>
{
let
TEST_DIR
beforeEach
(
(
done
)
=>
{
TEST_DIR
=
path
.
join
(
os
.
tmpdir
(
)
,
'jsonfile-tests-readfile-sync'
)
rimraf
.
sync
(
TEST_DIR
)
fs
.
mkdir
(
TEST_DIR
,
done
)
}
)
afterEach
(
(
done
)
=>
{
rimraf
.
sync
(
TEST_DIR
)
done
(
)
}
)
it
(
'should read and parse JSON'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile3.json'
)
const
obj
=
{
name
:
'JP'
}
fs
.
writeFileSync
(
file
,
JSON
.
stringify
(
obj
)
)
try
{
const
obj2
=
jf
.
readFileSync
(
file
)
assert
.
strictEqual
(
obj2
.
name
,
obj
.
name
)
}
catch
(
err
)
{
assert
(
err
)
}
}
)
describe
(
'> when invalid JSON'
,
(
)
=>
{
it
(
'should include the filename in the error'
,
(
)
=>
{
const
fn
=
'somefile.json'
const
file
=
path
.
join
(
TEST_DIR
,
fn
)
fs
.
writeFileSync
(
file
,
'{'
)
assert
.
throws
(
(
)
=>
{
jf
.
readFileSync
(
file
)
}
,
(
err
)
=>
{
assert
(
err
instanceof
Error
)
assert
(
err
.
message
.
match
(
fn
)
)
return
true
}
)
}
)
}
)
describe
(
'> when invalid JSON and throws set to false'
,
(
)
=>
{
it
(
'should return null'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile4-invalid.json'
)
const
data
=
'{not valid JSON'
fs
.
writeFileSync
(
file
,
data
)
assert
.
throws
(
(
)
=>
{
jf
.
readFileSync
(
file
)
}
)
const
obj
=
jf
.
readFileSync
(
file
,
{
throws
:
false
}
)
assert
.
strictEqual
(
obj
,
null
)
}
)
}
)
describe
(
'> when invalid JSON and throws set to true'
,
(
)
=>
{
it
(
'should throw an exception'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile4-invalid.json'
)
const
data
=
'{not valid JSON'
fs
.
writeFileSync
(
file
,
data
)
assert
.
throws
(
(
)
=>
{
jf
.
readFileSync
(
file
,
{
throws
:
true
}
)
}
)
}
)
}
)
describe
(
'> when json file is missing and throws set to false'
,
(
)
=>
{
it
(
'should return null'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile4-invalid.json'
)
const
obj
=
jf
.
readFileSync
(
file
,
{
throws
:
false
}
)
assert
.
strictEqual
(
obj
,
null
)
}
)
}
)
describe
(
'> when json file is missing and throws set to true'
,
(
)
=>
{
it
(
'should throw an exception'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile4-invalid.json'
)
assert
.
throws
(
(
)
=>
{
jf
.
readFileSync
(
file
,
{
throws
:
true
}
)
}
)
}
)
}
)
describe
(
'> when JSON reviver is set'
,
(
)
=>
{
it
(
'should transform the JSON'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile.json'
)
const
sillyReviver
=
function
(
k
,
v
)
{
if
(
typeof
v
!==
'string'
)
return
v
if
(
v
.
indexOf
(
'date:'
)
<
0
)
return
v
return
new
Date
(
v
.
split
(
'date:'
)
[
1
]
)
}
const
obj
=
{
name
:
'jp'
,
day
:
'date:2015-06-19T11:41:26.815Z'
}
fs
.
writeFileSync
(
file
,
JSON
.
stringify
(
obj
)
)
const
data
=
jf
.
readFileSync
(
file
,
{
reviver
:
sillyReviver
}
)
assert
.
strictEqual
(
data
.
name
,
'jp'
)
assert
(
data
.
day
instanceof
Date
)
assert
.
strictEqual
(
data
.
day
.
toISOString
(
)
,
'2015-06-19T11:41:26.815Z'
)
}
)
}
)
describe
(
'> when passing encoding string as option'
,
(
)
=>
{
it
(
'should not throw an error'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile.json'
)
const
obj
=
{
name
:
'jp'
}
fs
.
writeFileSync
(
file
,
JSON
.
stringify
(
obj
)
)
let
data
try
{
data
=
jf
.
readFileSync
(
file
,
'utf8'
)
}
catch
(
err
)
{
assert
.
ifError
(
err
)
}
assert
.
strictEqual
(
data
.
name
,
'jp'
)
}
)
}
)
describe
(
'> w/ BOM'
,
(
)
=>
{
it
(
'should properly parse'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'file-bom.json'
)
const
obj
=
{
name
:
'JP'
}
fs
.
writeFileSync
(
file
,
`\uFEFF
${
JSON
.
stringify
(
obj
)
}
`
)
const
data
=
jf
.
readFileSync
(
file
)
assert
.
deepStrictEqual
(
obj
,
data
)
}
)
}
)
}
)
Back
|
FazBrowse Home
|
New Git URL