FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-jsonfile/test/write-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
/
write-file-sync.test.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (103 loc) · 3.71 KB
Breadcrumbs
node-jsonfile
/
test
/
write-file-sync.test.js
Copy path
File metadata and controls
119 lines (103 loc) · 3.71 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
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
(
'+ writeFileSync()'
,
(
)
=>
{
let
TEST_DIR
beforeEach
(
(
done
)
=>
{
TEST_DIR
=
path
.
join
(
os
.
tmpdir
(
)
,
'jsonfile-tests-writefile-sync'
)
rimraf
.
sync
(
TEST_DIR
)
fs
.
mkdir
(
TEST_DIR
,
done
)
}
)
afterEach
(
(
done
)
=>
{
rimraf
.
sync
(
TEST_DIR
)
done
(
)
}
)
it
(
'should serialize the JSON and write it to file'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile4.json'
)
const
obj
=
{
name
:
'JP'
}
jf
.
writeFileSync
(
file
,
obj
)
const
data
=
fs
.
readFileSync
(
file
,
'utf8'
)
const
obj2
=
JSON
.
parse
(
data
)
assert
.
strictEqual
(
obj2
.
name
,
obj
.
name
)
assert
.
strictEqual
(
data
[
data
.
length
-
1
]
,
'\n'
)
assert
.
strictEqual
(
data
,
'{"name":"JP"}\n'
)
}
)
describe
(
'> when JSON replacer is set'
,
(
)
=>
{
it
(
'should replace JSON'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile.json'
)
const
sillyReplacer
=
function
(
k
,
v
)
{
if
(
!
(
v
instanceof
RegExp
)
)
return
v
return
`regex:
${
v
.
toString
(
)
}
`
}
const
obj
=
{
name
:
'jp'
,
reg
:
/
h
e
l
l
o
/
g
}
jf
.
writeFileSync
(
file
,
obj
,
{
replacer
:
sillyReplacer
}
)
const
data
=
JSON
.
parse
(
fs
.
readFileSync
(
file
)
)
assert
.
strictEqual
(
data
.
name
,
'jp'
)
assert
.
strictEqual
(
typeof
data
.
reg
,
'string'
)
assert
.
strictEqual
(
data
.
reg
,
'regex:/hello/g'
)
}
)
}
)
describe
(
'> when spaces passed as an option'
,
(
)
=>
{
it
(
'should write file with spaces'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile.json'
)
const
obj
=
{
name
:
'JP'
}
jf
.
writeFileSync
(
file
,
obj
,
{
spaces
:
8
}
)
const
data
=
fs
.
readFileSync
(
file
,
'utf8'
)
assert
.
strictEqual
(
data
,
`
${
JSON
.
stringify
(
obj
,
null
,
8
)
}
\n`
)
}
)
it
(
'should use EOL override'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile.json'
)
const
obj
=
{
name
:
'JP'
}
jf
.
writeFileSync
(
file
,
obj
,
{
spaces
:
2
,
EOL
:
'***'
}
)
const
data
=
fs
.
readFileSync
(
file
,
'utf8'
)
assert
.
strictEqual
(
data
,
'{*** "name": "JP"***}***'
)
}
)
}
)
describe
(
'> when passing encoding string as options'
,
(
)
=>
{
it
(
'should not error'
,
(
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile6.json'
)
const
obj
=
{
name
:
'jp'
}
jf
.
writeFileSync
(
file
,
obj
,
'utf8'
)
const
data
=
fs
.
readFileSync
(
file
,
'utf8'
)
assert
.
strictEqual
(
data
,
`
${
JSON
.
stringify
(
obj
)
}
\n`
)
}
)
}
)
describe
(
'> when EOF option is set to a falsey value'
,
(
)
=>
{
beforeEach
(
(
done
)
=>
{
TEST_DIR
=
path
.
join
(
os
.
tmpdir
(
)
,
'jsonfile-tests-writefile-sync'
)
rimraf
.
sync
(
TEST_DIR
)
fs
.
mkdir
(
TEST_DIR
,
done
)
}
)
afterEach
(
(
done
)
=>
{
rimraf
.
sync
(
TEST_DIR
)
done
(
)
}
)
it
(
'should not have a the EOL symbol at the end of file'
,
(
done
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile2.json'
)
const
obj
=
{
name
:
'jp'
}
jf
.
writeFileSync
(
file
,
obj
,
{
finalEOL
:
false
}
)
const
rawData
=
fs
.
readFileSync
(
file
,
'utf8'
)
const
data
=
JSON
.
parse
(
rawData
)
assert
.
strictEqual
(
rawData
[
rawData
.
length
-
1
]
,
'}'
)
assert
.
strictEqual
(
data
.
name
,
obj
.
name
)
done
(
)
}
)
it
(
'should have a the EOL symbol at the end of file when finalEOL is a truth value in options'
,
(
done
)
=>
{
const
file
=
path
.
join
(
TEST_DIR
,
'somefile2.json'
)
const
obj
=
{
name
:
'jp'
}
jf
.
writeFileSync
(
file
,
obj
,
{
finalEOL
:
true
}
)
const
rawData
=
fs
.
readFileSync
(
file
,
'utf8'
)
const
data
=
JSON
.
parse
(
rawData
)
assert
.
strictEqual
(
rawData
[
rawData
.
length
-
1
]
,
'\n'
)
assert
.
strictEqual
(
data
.
name
,
obj
.
name
)
done
(
)
}
)
}
)
}
)
Back
|
FazBrowse Home
|
New Git URL