test: simplify vm-module-errors test · nodejs/node@3d6533e (original) (raw)

`@@ -8,26 +8,6 @@ const assert = require('assert');

`

8

8

``

9

9

`const { SourceTextModule, createContext } = require('vm');

`

10

10

``

11

``

`-

async function expectsRejection(fn, settings) {

`

12

``

`-

const validateError = common.expectsError(settings);

`

13

``

`-

// Retain async context.

`

14

``

`-

const storedError = new Error('Thrown from:');

`

15

``

`-

try {

`

16

``

`-

await fn();

`

17

``

`-

} catch (err) {

`

18

``

`-

try {

`

19

``

`-

validateError(err);

`

20

``

`-

} catch (validationError) {

`

21

``

`-

console.error(validationError);

`

22

``

`-

console.error('Original error:');

`

23

``

`-

console.error(err);

`

24

``

`-

throw storedError;

`

25

``

`-

}

`

26

``

`-

return;

`

27

``

`-

}

`

28

``

`-

assert.fail('Missing expected exception');

`

29

``

`-

}

`

30

``

-

31

11

`async function createEmptyLinkedModule() {

`

32

12

`const m = new SourceTextModule('');

`

33

13

`await m.link(common.mustNotCall());

`

`@@ -57,19 +37,19 @@ async function checkArgType() {

`

57

37

`for (const invalidLinker of [

`

58

38

`0, 1, undefined, null, true, 'str', {}, Symbol.iterator

`

59

39

`]) {

`

60

``

`-

await expectsRejection(async () => {

`

``

40

`+

await assert.rejects(async () => {

`

61

41

`const m = new SourceTextModule('');

`

62

42

`await m.link(invalidLinker);

`

63

43

`}, {

`

64

44

`code: 'ERR_INVALID_ARG_TYPE',

`

65

``

`-

type: TypeError

`

``

45

`+

name: 'TypeError'

`

66

46

`});

`

67

47

`}

`

68

48

`}

`

69

49

``

70

50

`// Check methods/properties can only be used under a specific state.

`

71

51

`async function checkModuleState() {

`

72

``

`-

await expectsRejection(async () => {

`

``

52

`+

await assert.rejects(async () => {

`

73

53

`const m = new SourceTextModule('');

`

74

54

`await m.link(common.mustNotCall());

`

75

55

`assert.strictEqual(m.linkingStatus, 'linked');

`

`@@ -78,7 +58,7 @@ async function checkModuleState() {

`

78

58

`code: 'ERR_VM_MODULE_ALREADY_LINKED'

`

79

59

`});

`

80

60

``

81

``

`-

await expectsRejection(async () => {

`

``

61

`+

await assert.rejects(async () => {

`

82

62

`const m = new SourceTextModule('');

`

83

63

`m.link(common.mustNotCall());

`

84

64

`assert.strictEqual(m.linkingStatus, 'linking');

`

`@@ -94,15 +74,14 @@ async function checkModuleState() {

`

94

74

`code: 'ERR_VM_MODULE_NOT_LINKED'

`

95

75

`});

`

96

76

``

97

``

`-

await expectsRejection(async () => {

`

``

77

`+

await assert.rejects(async () => {

`

98

78

`const m = new SourceTextModule('import "foo";');

`

99

79

`try {

`

100

80

`await m.link(common.mustCall(() => ({})));

`

101

81

`} catch {

`

102

82

`assert.strictEqual(m.linkingStatus, 'errored');

`

103

83

`m.instantiate();

`

104

84

`}

`

105

``

`-

assert.fail('Unreachable');

`

106

85

`}, {

`

107

86

`code: 'ERR_VM_MODULE_NOT_LINKED'

`

108

87

`});

`

`@@ -124,15 +103,15 @@ async function checkModuleState() {

`

124

103

`await m.evaluate();

`

125

104

`}

`

126

105

``

127

``

`-

await expectsRejection(async () => {

`

``

106

`+

await assert.rejects(async () => {

`

128

107

`const m = new SourceTextModule('');

`

129

108

`await m.evaluate();

`

130

109

`}, {

`

131

110

`code: 'ERR_VM_MODULE_STATUS',

`

132

111

`message: 'Module status must be one of instantiated, evaluated, and errored'

`

133

112

`});

`

134

113

``

135

``

`-

await expectsRejection(async () => {

`

``

114

`+

await assert.rejects(async () => {

`

136

115

`const m = new SourceTextModule('');

`

137

116

`await m.evaluate(false);

`

138

117

`}, {

`

`@@ -141,7 +120,7 @@ async function checkModuleState() {

`

141

120

`'Received type boolean'

`

142

121

`});

`

143

122

``

144

``

`-

await expectsRejection(async () => {

`

``

123

`+

await assert.rejects(async () => {

`

145

124

`const m = await createEmptyLinkedModule();

`

146

125

`await m.evaluate();

`

147

126

`}, {

`

`@@ -157,7 +136,7 @@ async function checkModuleState() {

`

157

136

`message: 'Module status must be errored'

`

158

137

`});

`

159

138

``

160

``

`-

await expectsRejection(async () => {

`

``

139

`+

await assert.rejects(async () => {

`

161

140

`const m = await createEmptyLinkedModule();

`

162

141

`m.instantiate();

`

163

142

`await m.evaluate();

`

`@@ -175,7 +154,7 @@ async function checkModuleState() {

`

175

154

`message: 'Module status must not be uninstantiated or instantiating'

`

176

155

`});

`

177

156

``

178

``

`-

await expectsRejection(async () => {

`

``

157

`+

await assert.rejects(async () => {

`

179

158

`const m = await createEmptyLinkedModule();

`

180

159

`m.namespace;

`

181

160

`}, {

`

`@@ -186,20 +165,19 @@ async function checkModuleState() {

`

186

165

``

187

166

`// Check link() fails when the returned module is not valid.

`

188

167

`async function checkLinking() {

`

189

``

`-

await expectsRejection(async () => {

`

``

168

`+

await assert.rejects(async () => {

`

190

169

`const m = new SourceTextModule('import "foo";');

`

191

170

`try {

`

192

171

`await m.link(common.mustCall(() => ({})));

`

193

172

`} catch (err) {

`

194

173

`assert.strictEqual(m.linkingStatus, 'errored');

`

195

174

`throw err;

`

196

175

`}

`

197

``

`-

assert.fail('Unreachable');

`

198

176

`}, {

`

199

177

`code: 'ERR_VM_MODULE_NOT_MODULE'

`

200

178

`});

`

201

179

``

202

``

`-

await expectsRejection(async () => {

`

``

180

`+

await assert.rejects(async () => {

`

203

181

`const c = createContext({ a: 1 });

`

204

182

`const foo = new SourceTextModule('', { context: c });

`

205

183

`await foo.link(common.mustNotCall());

`

`@@ -210,12 +188,11 @@ async function checkLinking() {

`

210

188

`assert.strictEqual(bar.linkingStatus, 'errored');

`

211

189

`throw err;

`

212

190

`}

`

213

``

`-

assert.fail('Unreachable');

`

214

191

`}, {

`

215

192

`code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'

`

216

193

`});

`

217

194

``

218

``

`-

await expectsRejection(async () => {

`

``

195

`+

await assert.rejects(async () => {

`

219

196

`const erroredModule = new SourceTextModule('import "foo";');

`

220

197

`try {

`

221

198

`await erroredModule.link(common.mustCall(() => ({})));

`